/**
*中文附件名_回信地址_友好名称
*
*/
public class ComplexMail {
public static void main(String[] args) throws Exception{
// System.setProperty("socksProxyHost", "proxy2.lh.petrochina");
// System.setProperty("socksProxyPort", "8080");
Properties props = new Properties();
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", "smtp.sohu.com");
Session session = Session.getInstance(props,
new Authenticator()
{
@Override/*策略模式,代码封装*/
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication("xxssyyyyssxx","xsy881026");
}
}
);
session.setDebug(true);
MimeMessage msg = new MimeMessage(session);
/*发送人*/
msg.setFrom(new InternetAddress("[email protected]"));
msg.setSubject("复杂邮件生产");
/*回复人,如果不设置表示回复给发送人*/
msg.setReplyTo(new Address[]{new InternetAddress("[email protected]")});
//msg.setRecipients(RecipientType.TO,InternetAddress.parse(MimeUtility.encodeText("熊诗言") + " <[email protected]>," + MimeUtility.encodeText("熊诗言") + " <[email protected]>"));
msg.setRecipients(RecipientType.TO,InternetAddress.parse("[email protected]"));
//邮件是一个multipart/mixed复杂对象
/*设置为mixed,related,alternative不然会出现ParseException*/
MimeMultipart msgMultipart = new MimeMultipart("mixed");
msg.setContent(msgMultipart);
/*附件1*/
MimeBodyPart attch1 = new MimeBodyPart();
/*附件2*/
MimeBodyPart attch2 = new MimeBodyPart();
/*具体内容*/
MimeBodyPart content = new MimeBodyPart();
msgMultipart.addBodyPart(attch1);
msgMultipart.addBodyPart(attch2);
msgMultipart.addBodyPart(content);
/*附件1的设置*/
DataSource ds1 = new FileDataSource(
"D:\\Users\\熊诗言\\Desktop\\javamail\\src\\resource\\Java培训.txt"
);
DataHandler dh1 = new DataHandler(ds1 );
attch1.setDataHandler(dh1);
/*使用MimeUtility.encodeText进行中文编码,否则显示不出来*/
attch1.setFileName(
MimeUtility.encodeText("java培训.txt")
//首先你的中文编码是什么?然后对中文编码的二进制进行QuotedPrintable或者Base64编码
//=?UTF-8?Q?.....?=
//=?GBK?B?.....?=
);
/*附件2的设置*/
DataSource ds2 = new FileDataSource(
"D:\\Users\\熊诗言\\Desktop\\javamail\\src\\resource\\slogo.gif"
);
DataHandler dh2 = new DataHandler(ds2 );
attch2.setDataHandler(dh2);
attch2.setFileName("slogo.gif");
/*content是一个multipart/related复杂对象*/
MimeMultipart bodyMultipart = new MimeMultipart("related");
content.setContent(bodyMultipart);
/*内容有html和图片*/
MimeBodyPart htmlPart = new MimeBodyPart(); /*这个也可以设置成为纯文本和HTML的alternative*/
MimeBodyPart gifPart = new MimeBodyPart();
bodyMultipart.addBodyPart(htmlPart);
bodyMultipart.addBodyPart(gifPart);
/*html部分呢*/
htmlPart.setContent("你们的Java培训真的是最牛的吗?大家都这么说,我想跟你们比试一下!这可是我自己用程序生成和发送的邮件哦!<img src='cid:http://www.itcast.cn/logo.gif'>"
, "text/html;charset=gbk");
/*图片部分呢*/
DataSource gifds = new FileDataSource(
"D:\\Users\\熊诗言\\Desktop\\javamail\\src\\resource\\logo.gif"
);
DataHandler gifdh = new DataHandler(gifds);
gifPart.setDataHandler(gifdh);
gifPart.setHeader("Content-ID", "http://www.itcast.cn/logo.gif");//被别的引用CID:XXXXXX
/*内容生产完事儿了一定保存*/
msg.saveChanges();
/*保存到文件中*/
OutputStream ips = new FileOutputStream("demo2.eml");
msg.writeTo(ips);
ips.close();
Transport.send(msg);
}
}