0
点赞
收藏
分享

微信扫一扫

电子邮件系统 6----JavaMail发送带附件的电子邮件示例(2)

1. /**
2.  * CrazyItTest
3.  * JavaMail 创建带附件的电子邮件示例
4.  */
5. package
6. import
7. import
8. import
9. import
10. import
11. import
12. import
13. import
14. import
15. import
16. import
17. import
18. import
19. import
20. import
21. import
22. import
23. /**
24.  * @author Bill Tu
25.  * @since May 26, 2011(21:03:36 PM)
26.  *
27.  */
28. public class
29. private static MimeMessage getTextMessage(Session session) throws
30.         MessagingException, UnsupportedEncodingException{  
31. new
32. "iwtxokhtd@163.com";//发送方邮件地址 
33. "277515433@qq.com";//接收方邮件地址 
34.         
35. "带附件的邮件";//邮件主题,注意是中文的 
36.         
37. "<h1>欢迎啊</h1><img src="cid:my1.jpg" mce_src="cid:my1.jpg"/>";//cid为my1.jpg,下文会设置此cid 
38. new
39. new
40.       message.setSubject(subject);  
41. new Date());//发送时间 
42.         
43. "F://My头像.jpg");   
44. "F://nginx中文.txt");//注意附件名是中文的   
45. "F://nginx英文.doc");   
46.         
47. new MimeMultipart("mixed");//MIME消息头组合类型是mixed(html+附件) 
48.       mmp.addBodyPart(picBodyPart);  
49.       mmp.addBodyPart(attached1BodyPart);  
50.       mmp.addBodyPart(attached2BodyPart);  
51.         
52.       message.setContent(mmp);  
53.       message.saveChanges();  
54.         
55. return
56.     
57.  }  
58.    
59. /**
60.   * 处理文件名
61.   * 此处是针对Window下的。
62.   * @param filePath
63.   * @return
64.   */
65. private static
66.       String fileName=filePath;  
67. if(null !=filePath && !"".equals(filePath)){   
68. "//")+1);   
69.       }  
70. return
71.  }  
72.    
73.    
74. private static MimeBodyPart getAttachedBodyPart(String filePath) throws
75.         UnsupportedEncodingException{  
76. new
77. new
78. new
79.       String fileName=doHandlerFileName(filePath);  
80. //处理附件文件的中文名问题 
81. return
82.  }  
83.    
84. /**
85.   * 处理html加图片的类型(related)
86.   * @param content
87.   * @param picName
88.   * @return
89.   * @throws MessagingException
90.   */
91. private static MimeBodyPart getPicBodyPart(String content,String picName) throws
92. new
93.         
94. new MimeMultipart("related");//此处MIME消息头组合类型为related 
95. new
96. "text/html;charset=gb2312");//因正文内容中有中文 
97.         
98.       mmp.addBodyPart(contented);  
99.         
100. new
101. new
102. new
103. "my1.jpg");//设置contentId 
104.         
105.       mmp.addBodyPart(picBodyPart);  
106.         
107.       contentPart.setContent(mmp);  
108.         
109. return
110.  }  
111.    
112.    
113. public static void main(String[] args) throws
114.         MessagingException, FileNotFoundException, IOException {  
115. new
116.       MimeMessage message=getTextMessage(session);  
117. new FileOutputStream("F://mailtext.eml"));   
118.     
119.  }  
120. }  
/** * CrazyItTest * JavaMail 创建带附件的电子邮件示例 */ package com.labci.javamail.test; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.Date; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.Message.RecipientType; import javax.mail.MessagingException; import javax.mail.Session; import javax.mail.internet.AddressException; 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; /** * @author Bill Tu * @since May 26, 2011(21:03:36 PM) * */ public class ComplexMailTest { private static MimeMessage getTextMessage(Session session) throws AddressException, MessagingException, UnsupportedEncodingException{ MimeMessage message=new MimeMessage(session); String from="iwtxokhtd@163.com";//发送方邮件地址 String to="277515433@qq.com";//接收方邮件地址 String subject="带附件的邮件";//邮件主题,注意是中文的 String content="<h1>欢迎啊</h1><img src="cid:my1.jpg" mce_src="cid:my1.jpg"/>";//cid为my1.jpg,下文会设置此cid message.setFrom(new InternetAddress(from)); message.setRecipient(RecipientType.TO, new InternetAddress(to)); message.setSubject(subject); message.setSentDate(new Date());//发送时间 MimeBodyPart picBodyPart=getPicBodyPart(content,"F://My头像.jpg"); MimeBodyPart attached1BodyPart=getAttachedBodyPart("F://nginx中文.txt");//注意附件名是中文的 MimeBodyPart attached2BodyPart=getAttachedBodyPart("F://nginx英文.doc"); MimeMultipart mmp=new MimeMultipart("mixed");//MIME消息头组合类型是mixed(html+附件) mmp.addBodyPart(picBodyPart); mmp.addBodyPart(attached1BodyPart); mmp.addBodyPart(attached2BodyPart); message.setContent(mmp); message.saveChanges(); return message; } /** * 处理文件名 * 此处是针对Window下的。 * @param filePath * @return */ private static String doHandlerFileName(String filePath){ String fileName=filePath; if(null !=filePath && !"".equals(filePath)){ fileName=filePath.substring(filePath.lastIndexOf("//")+1); } return fileName; } private static MimeBodyPart getAttachedBodyPart(String filePath) throws MessagingException, UnsupportedEncodingException{ MimeBodyPart attached=new MimeBodyPart(); FileDataSource fds=new FileDataSource(filePath); attached.setDataHandler(new DataHandler(fds)); String fileName=doHandlerFileName(filePath); attached.setFileName(MimeUtility.encodeWord(fileName));//处理附件文件的中文名问题 return attached; } /** * 处理html加图片的类型(related) * @param content * @param picName * @return * @throws MessagingException */ private static MimeBodyPart getPicBodyPart(String content,String picName) throws MessagingException{ MimeBodyPart contentPart=new MimeBodyPart(); MimeMultipart mmp=new MimeMultipart("related");//此处MIME消息头组合类型为related MimeBodyPart contented=new MimeBodyPart(); contented.setContent(content,"text/html;charset=gb2312");//因正文内容中有中文 mmp.addBodyPart(contented); MimeBodyPart picBodyPart=new MimeBodyPart(); FileDataSource fds=new FileDataSource(picName); picBodyPart.setDataHandler(new DataHandler(fds)); picBodyPart.setContentID("my1.jpg");//设置contentId mmp.addBodyPart(picBodyPart); contentPart.setContent(mmp); return contentPart; } public static void main(String[] args) throws AddressException, MessagingException, FileNotFoundException, IOException { Session session=Session.getDefaultInstance(new Properties()); MimeMessage message=getTextMessage(session); message.writeTo(new FileOutputStream("F://mailtext.eml")); } } 
 
用outlook打开保存在F盘的mailtest.eml文件,查看一下邮件内容:

举报

相关推荐

0 条评论