少年阿宾

那些青春的岁月

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  500 Posts :: 0 Stories :: 135 Comments :: 0 Trackbacks
邮件发送和接收已经全部实现,大家在转发市请注明出处(我这也是参考了N多大牛的作品):

//发送邮件类:

import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;

public class SendMail {
    
    public boolean Send(MailInfo mailInfo)throws Exception{
        Properties props=new Properties();
        //设置发送邮件的邮件服务器的属性(这里使用网易的smtp服务器)
        props.put("mail.smtp.host", mailInfo.getHost());
        //需要经过授权,也就是有户名和密码的校验,这样才能通过验证(一定要有这一条)
        props.put("mail.smtp.auth", mailInfo.getValidate());
        Session session=Session.getDefaultInstance(props);// 用刚刚设置好的props对象构建一个session
        session.setDebug(true);
        MimeMessage message=new MimeMessage(session);
        try{
            message.setFrom(new InternetAddress(mailInfo.getFromAddress()));
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(mailInfo.getToAddress()));
            message.setSubject(mailInfo.getSubject());
            message.setSentDate(mailInfo.getSentdate());
            //向multipart对象中添加邮件的各个部分内容,包括文本内容和附件
            Multipart multipart=new MimeMultipart();
            BodyPart contentPart=new MimeBodyPart();
            contentPart.setText(mailInfo.getContent());
            multipart.addBodyPart(contentPart);
            //添加附件
            if(mailInfo.getFilepath()!=null){
                BodyPart messageBodyPart=new MimeBodyPart();
                DataSource source=new FileDataSource(mailInfo.getFilepath());
                
                messageBodyPart.setDataHandler(new DataHandler(source));
                sun.misc.BASE64Encoder enc=new sun.misc.BASE64Encoder();
                String fname=mailInfo.getFilename();
                if(fname!=null){
                    messageBodyPart.setFileName("=?GBK?B?"+enc.encode(fname.getBytes())+"?=");
            //        messageBodyPart.setFileName(MimeUtility.encodeText(fname));
                    multipart.addBodyPart(messageBodyPart);
                }else{
                    String lname=mailInfo.getFilepath().substring(mailInfo.getFilepath().lastIndexOf("\\")+1, mailInfo.getFilepath().length());
                    System.out.println(lname);
                    messageBodyPart.setFileName("=?GBK?B?"+enc.encode(lname.getBytes())+"?=");
            //        messageBodyPart.setFileName(MimeUtility.encodeText(lname));
                    multipart.addBodyPart(messageBodyPart);
                }
                
            }

            
            message.setContent(multipart);//text/plain表示纯文本内容
            message.saveChanges();
            Transport transport=session.getTransport("smtp");
            transport.connect(mailInfo.getHost(),mailInfo.getUsername(),mailInfo.getPassword());
            transport.sendMessage(message,message.getAllRecipients());
            transport.close();
            return true;
        }catch(Exception e){
            e.printStackTrace();
        }
        
        return false;
    }
}



//测试邮件发送类
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class SendTest {

    @Before
    public void setUp() throws Exception {
    }

    @After
    public void tearDown() throws Exception {
    }
    @Test
    public void mailTest(){
        SendMail mail=new SendMail();
        MailInfo mailInfo=new MailInfo();
        mailInfo.setContent("邮件内容44444444444444444444");
        mailInfo.setFilepath("E:\\Emotion\\Photo\\乳沟.jpg");
        String fname=mailInfo.getFilepath().substring(mailInfo.getFilepath().lastIndexOf("."), mailInfo.getFilepath().length());
        System.out.println(fname);
//        mailInfo.setFilename("希望"+fname);
        mailInfo.setFromAddress("leeposter@163.com");
        mailInfo.setHost("smtp.163.com");
        mailInfo.setSubject("邮件主题444444444444444444444");
        mailInfo.setToAddress("liposter@163.com");
        mailInfo.setUsername("leeposter");
        mailInfo.setValidate("true");
        mailInfo.setPassword("*********");
        try{
            boolean flag=mail.Send(mailInfo);
            if(flag==true){
                System.out.println("发送成功");
            }else{
                System.out.println("发送失败");
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        
        
    }

}
posted on 2011-10-17 23:56 abin 阅读(1843) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: