首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

“世界下最复杂的邮件”诞生了

2012-12-27 
“世界上最复杂的邮件”诞生了import java.io.FileNotFoundExceptionimport java.io.FileOutputStreamimpo

“世界上最复杂的邮件”诞生了

import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.UnsupportedEncodingException;import java.util.Properties;import javax.activation.DataHandler;import javax.activation.FileDataSource;import javax.mail.Message;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;public class MImemessageTest {/** * @throws MessagingException * @throws AddressException * @throws IOException * @throws FileNotFoundException * * * * * */ public static void main(String[] args) throws AddressException, MessagingException, FileNotFoundException, IOException { //创建邮件 MimeMessage message=new MimeMessage(Session.getInstance(new Properties())); //设置属性 message.setFrom(new InternetAddress("wangliaizq10000@sina.com")); message.setRecipient(Message.RecipientType.TO, new InternetAddress("wangliaizq@sohu.com")); message.setSubject("你猜"); //创建正文 MimeBodyPart text=new MimeBodyPart(); text.setContent("猜猜我是谁<img src='xx.jpg'>nicaicaia ", "text/html;charset=gb2312"); //创建图片 MimeBodyPart image=new MimeBodyPart(); DataHandler dh=new DataHandler(new FileDataSource("src\\2.jpg")); image.setDataHandler(dh); image.setContentID("xx.jpg"); //创建附件 MimeBodyPart accth=new MimeBodyPart(); DataHandler acc=new DataHandler(new FileDataSource("src\\大笑.mp3")); accth.setDataHandler(acc); String filename=acc.getName(); accth.setFileName(MimeUtility.encodeText(filename));//解决中文乱码 //描述关系 //图片与正文的描述关系 MimeMultipart mm=new MimeMultipart(); mm.addBodyPart(text); mm.addBodyPart(image); mm.setSubType("related"); //图片与正文的Bobypart MimeBodyPart part=new MimeBodyPart(); part.setContent(mm); //附件与以上的bobypary MimeMultipart m=new MimeMultipart(); m.addBodyPart(part); m.addBodyPart(accth); m.setSubType("mixed"); //把MIME消息设置到messages中 message.setContent(m); message.saveChanges(); message.writeTo(new FileOutputStream("D:\\55.eml")); } }

?

<!--EndFragment-->

热点排行