JavaMail创建带附件电子邮件示例
2011-05-26 21:10
381 查看
]/** * 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文件,查看一下邮件内容:
相关文章推荐
- JavaMail发送带附件的电子邮件示例
- javamail创建带图片和附件的邮件
- javamail模拟邮箱功能发送电子邮件-中级实战篇【新增附件发送方法】(javamail API电子邮件实例)
- javamail模拟邮箱功能获取邮件内容-中级实战篇【内容|附件下载方法】(javamail API电子邮件实例)
- JavaMail解析电子邮件初步示例(Web程序不完全版)
- javamail模拟邮箱功能获取邮件内容-中级实战篇【内容|附件下载方法】(javamail API电子邮件实例)
- 使用C#发送带附件的电子邮件的方法的代码示例分析
- JavaMail:创建内含附件、图文并茂的邮件
- MailBee.NET Objects发送电子邮件(SMTP)教程六:创建并发送带有附件的邮件
- 利用JavaMail创建邮件的示例
- JavaMail:创建内含附件、图文并茂的邮件
- Outlook 电子邮件附件时错误信息: " 无法创建文件 "
- javamail模拟邮箱功能发送电子邮件-中级实战篇【新增附件发送方法】(javamail API电子邮件实例)
- JavaMail:创建内含附件、图文并茂的邮件
- SMTP服务发送电子邮件示例的总结(带上传的附件)
- 利用JavaMail创建邮件的示例
- Outlook 电子邮件附件时错误信息: " 无法创建文件 "
- 当您尝试在 Outlook 中打开或保存电子邮件附件时出现错误消息:“无法创建文件
- 创建电子邮件链接
- DevExtreme常用示例:如何在Visual Studio中创建本地封装包