本地邮件(三)
package com.wp.mail; import java.io.FileOutputStream; import java.util.Properties; import javax.mail.Message; import javax.mail.Session; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; /**
package com.wp.mail; import java.io.FileOutputStream; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.Message; import javax.mail.Session; 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 admin * */ public class Mail03 { public static void main(String[] args) throws Exception { Properties properties = new Properties(); Session session = Session.getDefaultInstance(properties); MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress("18779740569@163.com")); message.addRecipients(Message.RecipientType.TO, "18779740569@163.com"); message.setSubject("这是复杂关系的邮件!"); // 文本 MimeBodyPart textPart = new MimeBodyPart(); textPart .setContent("哈哈哈哈<img src='cid:mm'>", "text/html;charset=UTF-8");// 这里的utf-8编码只对内容有效,而对附件名无效 // 图片 MimeBodyPart imagePart = new MimeBodyPart(); DataHandler dh = new DataHandler(new FileDataSource("D:/1.jpeg")); imagePart.setDataHandler(dh); imagePart.setContentID("mm"); // 文本和图片合体,合体后再变为BodyPart部分 MimeMultipart mpart1 = new MimeMultipart(); mpart1.addBodyPart(textPart); mpart1.addBodyPart(imagePart); mpart1.setSubType("related"); MimeBodyPart textImagePart = new MimeBodyPart(); textImagePart.setContent(mpart1); // 附件 MimeBodyPart attachmentPart = new MimeBodyPart(); dh = new DataHandler(new FileDataSource("D:/附件.zip")); String filename = dh.getName();// 获取文件名 attachmentPart.setDataHandler(dh); attachmentPart.setFileName(MimeUtility.encodeText(filename));// 手动设置文件名,注意文件名的中文乱码问题 // 全部合体 MimeMultipart mpart2 = new MimeMultipart(); mpart2.addBodyPart(textImagePart); mpart2.addBodyPart(attachmentPart); mpart2.setSubType("mixed");// 复杂关系 message.setContent(mpart2); message.saveChanges(); message.writeTo(new FileOutputStream("D:/3.eml")); } }
* 一封纯文本邮件 * * @author admin * */ public class Mail01 { public static void main(String[] args) throws Exception { Properties properties = new Properties();// 环境变量设置,发送邮件时才需要 Session session = Session.getDefaultInstance(properties);// MimeMessage message = new MimeMessage(session);// MIME是互联网邮件扩展类型 message.setFrom(new InternetAddress("18779740569@163.com"));// 发件人 message.setRecipients(Message.RecipientType.TO, "18779740569@163.com");// 收件人 message.setSubject("这是第一封java邮件!");// 设置主题 // 设置邮件内容 message.setText("嘿嘿,我写的!"); // 放到本地磁盘 message.writeTo(new FileOutputStream("D:/1.eml")); } }
package com.wp.mail; import java.io.FileOutputStream; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.Message; import javax.mail.Session; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; /** * 一封包含图片邮件 * * @author admin * */ public class Mail02 { public static void main(String[] args) throws Exception { Properties properties = new Properties();// 环境变量设置,发送邮件时才需要 Session session = Session.getDefaultInstance(properties);// MimeMessage message = new MimeMessage(session);// MIME是互联网邮件扩展类型 message.setFrom(new InternetAddress("18779740569@163.com"));// 发件人 message.setRecipients(Message.RecipientType.TO, "18779740569@163.com");// 收件人 message.setSubject("这是一封包含图片的邮件!");// 设置主题 // 文本 MimeBodyPart textPart = new MimeBodyPart(); textPart.setContent("aaaaa<img src='cid:mm'>aaaaa", "text/html");// cid表示Content-ID,一个标识。text/html是MIME其中的一种类型 // 图片 MimeBodyPart imgPart = new MimeBodyPart(); // 把磁盘上的文件读到邮件中来:借助jaf框架 DataHandler dh = new DataHandler(new FileDataSource("D:/1.jpeg"));// 数据处理这张图片 // 通过jaf读取到的文件,不需要设置其MIME类型,jaf会自动探测到 imgPart.setDataHandler(dh);// 设置处理好的图片 imgPart.setContentID("mm");// 将图片放到文本中 // 描述二者的关系 MimeMultipart maMultipart = new MimeMultipart(); maMultipart.addBodyPart(textPart); maMultipart.addBodyPart(imgPart); maMultipart.setSubType("related");// related说明二者有联系 message.setContent(maMultipart); message.saveChanges(); message.writeTo(new FileOutputStream("D:/2.eml")); } }