java发送邮件 发送带附件的邮件

  1 import java.io.File;
  2 import java.util.Date;
  3  
  4 import javax.activation.DataHandler;
  5 import javax.activation.FileDataSource;
  6 import javax.mail.Address;
  7 import javax.mail.BodyPart;
  8 import javax.mail.Message;
  9 import javax.mail.Multipart;
 10 import javax.mail.Session;
 11 import javax.mail.Transport;
 12 import javax.mail.internet.InternetAddress;
 13 import javax.mail.internet.MimeBodyPart;
 14 import javax.mail.internet.MimeMessage;
 15 import javax.mail.internet.MimeMultipart;
 16 import javax.mail.internet.MimeUtility;
 17  
 18 /**
 19  * 发送带附件的邮件
 20  * 
 21  * 需要导入mail.jar
 22  */
 23 public class AttachmentMailSender {
 24  
 25     public static boolean sendMail(MailSenderInfo mailInfo) {
 26         // 判断是否需要身份认证
 27         MyAuthenticator authenticator = null;
 28         if (mailInfo.isValidate()) {
 29             // 如果需要身份认证,则创建一个密码验证器
 30             authenticator = new MyAuthenticator(mailInfo.getUserName(),
 31                     mailInfo.getPassword());
 32         }
 33         // 根据邮件发送的属性和密码验证器构造一个发送邮件的session
 34         Session sendMailSession = Session.getInstance(mailInfo.getProperties(),
 35                 authenticator);
 36         try {
 37             // 根据session创建一个邮件消息
 38             Message mailMessage = new MimeMessage(sendMailSession);
 39             // 创建邮件发送者地址
 40             Address from = new InternetAddress(mailInfo.getFromAddress());
 41             // 设置邮件消息的发送者
 42             mailMessage.setFrom(from);
 43             // 创建邮件的接收者地址,并设置到邮件消息中
 44             Address to = new InternetAddress(mailInfo.getToAddress());
 45             mailMessage.setRecipient(Message.RecipientType.TO, to);
 46             // 设置邮件消息的主题
 47             mailMessage.setSubject(mailInfo.getSubject());
 48             // 设置邮件消息发送的时间
 49             mailMessage.setSentDate(new Date());
 50  
 51             // MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象
 52             Multipart mainPart = new MimeMultipart();
 53             // 创建一个包含HTML内容的MimeBodyPart
 54             BodyPart html = new MimeBodyPart();
 55             // 设置HTML内容
 56             html.setContent(mailInfo.getContent(), "text/html; charset=GBK");
 57             mainPart.addBodyPart(html);
 58             // 为邮件添加附件
 59             String[] attachFileNames = mailInfo.getAttachFileNames();
 60             if (attachFileNames != null && attachFileNames.length > 0) {
 61                 // 存放邮件附件的MimeBodyPart
 62                 MimeBodyPart attachment = null;
 63                 File file = null;
 64                 for (int i = 0; i < attachFileNames.length; i++) {
 65                     attachment = new MimeBodyPart();
 66                     // 根据附件文件创建文件数据源
 67                     file = new File(attachFileNames[i]);
 68                     FileDataSource fds = new FileDataSource(file);
 69                     attachment.setDataHandler(new DataHandler(fds));
 70                     // 为附件设置文件名
 71                     attachment.setFileName(MimeUtility.encodeWord(
 72                             file.getName(), "GBK", null));
 73                     mainPart.addBodyPart(attachment);
 74                 }
 75             }
 76             // 将MiniMultipart对象设置为邮件内容
 77             mailMessage.setContent(mainPart);
 78             // 发送邮件
 79             Transport.send(mailMessage);
 80             return true;
 81  
 82         } catch (Exception e) {
 83             e.printStackTrace();
 84             return false;
 85         }
 86     }
 87  
 88     public static void main(String[] args) {
 89         // 创建邮件信息
 90         MailSenderInfo mailInfo = new MailSenderInfo();
 91         mailInfo.setMailServerHost("smtp.sina.com.cn");
 92         mailInfo.setMailServerPort("25");
 93         mailInfo.setValidate(true);
 94         mailInfo.setUserName("***");
 95         mailInfo.setPassword("***");
 96         mailInfo.setFromAddress("***@sina.com");
 97         mailInfo.setToAddress("***@163.com");
 98         mailInfo.setSubject("MyMail测试");
 99         mailInfo.setContent("我的邮件测试\n\rMy test mail\n\r");
100  
101         String[] fileNames = new String[3];
102         fileNames[0] = "C:/temp/new.txt";
103         fileNames[1] = "C:/temp/test.wav";
104         fileNames[2] = "C:/temp/mary_photo.jpg";
105         mailInfo.setAttachFileNames(fileNames);
106  
107         AttachmentMailSender.sendMail(mailInfo);
108     }
109 }
来自:http://www.open-open.com/code/view/1420037773921

 

posted @ 2015-04-17 13:50  Eddylon  阅读(403)  评论(0)    收藏  举报