博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java发送邮件 发送带附件的邮件
阅读量:4365 次
发布时间:2019-06-07

本文共 4419 字,大约阅读时间需要 14 分钟。

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

 

转载于:https://www.cnblogs.com/eddylon/p/4434732.html

你可能感兴趣的文章
hihocoder #1260 : String Problem I
查看>>
解决Delphi图形化界面的TEdit、TLable等组件手动拖拽固定大小,但是编译之后显示有差别的情况...
查看>>
Linux下安装MySQL
查看>>
webdriver之富文本,Firefox配置加载
查看>>
iOS开发笔记系列-基础7(C语言特性)
查看>>
cf 164 div2 解题报告
查看>>
最佳实践 | OceanBase事务引擎的技术创新
查看>>
unity中开启和关闭协同程序
查看>>
hdu_2089_不要62(数位DP)
查看>>
red and black(BFS)
查看>>
887. Super Egg Drop
查看>>
Properties类加载属性文件
查看>>
堆和栈&值类型和引用类型&拆箱和装箱
查看>>
HTML5外包团队:HTML5 Canvas使用教程
查看>>
内核调试日志打印宏
查看>>
C语言中格式化输出,四舍五入类型问题
查看>>
ListView
查看>>
WPF浏览器应用程序与JS的互调用(不用WebBrowser)
查看>>
bzoj2820: YY的GCD
查看>>
链表后续完善(一)
查看>>