相关文章:
目录
简介:
JavaMail,顾名思义,提供给开发者处理电子邮件相关的编程接口。它是Sun发布的用来处理email的API。它可以方便地执行一些常用的邮件传输。
使用JavaMail需要首先引入jar包:
<dependency> <groupId>javax.mail</groupId> <artifactId>javax.mail-api</artifactId> <version>1.6.0</version> </dependency> |
如果在Java SE环境里面测试还需要引入:
<dependency> <groupId>com.sun.mail</groupId> <artifactId>javax.mail</artifactId> <version>1.6.0</version> </dependency> |
如何使用将直接在代码中体现,以下代码将分为四个部分:
1.发送一封正文只包含文字的邮件
2.发送一封正文含有图片的文件
3.发送一封带附件的邮件
4.发送一封正文含有图片且包含附件的邮件
详细的介绍可以直接参考第一封邮件,其他三封都是以第一封为基础的。
一、发送正文只含文字的邮件
package com.zzs.commons.mail;
import java.util.Properties;
import javax.mail.Message; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage;
/** * 发送一封只包含文本的简单邮件 */ public class MailTest { public static void main(String[] args) throws Exception { Properties prop = new Properties(); prop.setProperty("mail.host", "smtp.sohu.com"); // 设置邮件服务器主机名 prop.setProperty("mail.transport.protocol", "smtp"); // 发送邮件协议名称 prop.setProperty("mail.smtp.auth", "true");// 发送服务器需要身份验证
// // 某些邮箱服务器要求 SMTP 连接需要使用 SSL 安全认证 // prop.setProperty("mail.smtp.port", "465"); // prop.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); // prop.setProperty("mail.smtp.socketFactory.fallback", "false"); // prop.setProperty("mail.smtp.socketFactory.port", "465");
// 使用JavaMail发送邮件的5个步骤 // 1、创建session Session session = Session.getInstance(prop); // 开启Session的debug模式,这样就可以查看到程序发送Email的运行状态 session.setDebug(true); // 2、通过session得到transport对象 Transport ts = session.getTransport(); // 3、使用邮箱的用户名和密码连上邮件服务器(用户名只需写@前面的即可) ts.connect("smtp.sohu.com", "用户名", "密码"); // 4、创建邮件 Message message = createSimpleMail(session); // 5、发送邮件 ts.sendMessage(message, message.getAllRecipients()); ts.close(); }
/** * 创建一封只包含文本的邮件 */ public static MimeMessage createSimpleMail(Session session) throws Exception { // 创建邮件对象 MimeMessage message = new MimeMessage(session);
// 指明邮件的发件人 message.setFrom(new InternetAddress("xxx@sohu.com")); // 指明邮件的收件人,现在发件人和收件人是一样的,那就是自己给自己发 message.setRecipient(Message.RecipientType.TO, new InternetAddress("xxx@163.com")); // // 发送给多个收件人 // message.setRecipients(Message.RecipientType.TO, new InternetAddress[] {}); // // Cc: 抄送(可选) // message.setRecipient(MimeMessage.RecipientType.CC, new InternetAddress("")); // // Bcc: 密送(可选) // message.setRecipient(MimeMessage.RecipientType.BCC, new InternetAddress("")); // 邮件的主题 message.setSubject("测试邮件"); // 邮件的文本内容 message.setText("Hello, this is the test mail.");
// 返回创建好的邮件对象 return message; } } |
二、发送正文含有图片的邮件
package com.zzs.commons.mail;
import java.util.Properties;
import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.Message; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart;
/** * 发送包含内嵌图片的邮件 */ public class MailTest2 { public static void main(String[] args) throws Exception {
Properties prop = new Properties(); prop.setProperty("mail.host", "smtp.sohu.com"); // 设置邮件服务器主机名 prop.setProperty("mail.transport.protocol", "smtp"); // 发送邮件协议名称 prop.setProperty("mail.smtp.auth", "true");// 发送服务器需要身份验证
// 使用JavaMail发送邮件的5个步骤 // 1、创建session Session session = Session.getInstance(prop); session.setDebug(true); // 2、通过session得到transport对象 Transport ts = session.getTransport(); ts.connect("smtp.sohu.com", "用户名", "密码"); // 4、创建邮件 Message message = createImageMail(session); // 5、发送邮件 ts.sendMessage(message, message.getAllRecipients()); ts.close(); }
/** * 生成一封邮件正文带图片的邮件 */ public static MimeMessage createImageMail(Session session) throws Exception { // 创建邮件对象 MimeMessage message = new MimeMessage(session);
// 设置邮件的基本信息 message.setFrom(new InternetAddress("xxx@sohu.com")); message.setRecipient(Message.RecipientType.TO, new InternetAddress("xxx@163.com")); message.setSubject("测试邮件");
// 准备邮件数据 // 准备邮件正文数据 // 创建文本“节点” MimeBodyPart text = new MimeBodyPart(); // 这里添加图片的方式是将整个图片包含到邮件内容中,实际上也可以以 http 链接的形式添加网络图片 text.setContent("这是一封邮件正文带图片<img src='cid:001.jpg'>的邮件", "text/html;charset=UTF-8");
// 准备图片数据 // 创建图片“节点” MimeBodyPart image = new MimeBodyPart(); DataHandler dh = new DataHandler(new FileDataSource("E:\\test\\data\\mail\\123.jpg"));// 读取本地文件 image.setDataHandler(dh);// 将图片数据添加到“节点” image.setContentID("001.jpg");// 为“节点”设置一个唯一编号(在文本“节点”将引用该ID)
// 设置 文本 和 图片 “节点”的关系 MimeMultipart mm = new MimeMultipart(); mm.addBodyPart(text); mm.addBodyPart(image); mm.setSubType("related"); // 关联关系
message.setContent(mm); message.saveChanges(); // // 将创建好的邮件写入到本地磁盘保存 // message.writeTo(new FileOutputStream("D:\\ImageMail.eml"));
return message; } } |
三、发送一封含有附件的邮件
package com.zzs.commons.mail;
import java.util.Properties;
import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.Message; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart;
/** * 发送包含附件的邮件 */ public class MailTest3 { public static void main(String[] args) throws Exception {
Properties prop = new Properties(); prop.setProperty("mail.host", "smtp.sohu.com"); // 设置邮件服务器主机名 prop.setProperty("mail.transport.protocol", "smtp"); // 发送邮件协议名称 prop.setProperty("mail.smtp.auth", "true");// 发送服务器需要身份验证
// 使用JavaMail发送邮件的5个步骤 // 1、创建session Session session = Session.getInstance(prop); session.setDebug(true); // 2、通过session得到transport对象 Transport ts = session.getTransport(); ts.connect("smtp.sohu.com", "用户名", "密码"); // 4、创建邮件 Message message = createAttachMail(session); // 5、发送邮件 ts.sendMessage(message, message.getAllRecipients()); ts.close(); }
/** * 创建一封带附件的邮件 */ public static MimeMessage createAttachMail(Session session) throws Exception { MimeMessage message = new MimeMessage(session);
// 设置邮件的基本信息 message.setFrom(new InternetAddress("xxx@sohu.com")); message.setRecipient(Message.RecipientType.TO, new InternetAddress("xxx@163.com")); message.setSubject("测试邮件");
// 创建邮件正文 MimeBodyPart text = new MimeBodyPart(); text.setContent("使用JavaMail创建的带附件的邮件", "text/html;charset=UTF-8");
// 创建附件 MimeBodyPart attach = new MimeBodyPart(); DataHandler dh = new DataHandler(new FileDataSource("E:\\test\\data\\mail\\123.jpg"));// 本地文件 attach.setDataHandler(dh); attach.setFileName(dh.getName()); // 附件名
// 创建容器描述数据关系 MimeMultipart mp = new MimeMultipart(); mp.addBodyPart(text);// 添加正文 mp.addBodyPart(attach);// 添加附件 mp.setSubType("mixed");
message.setContent(mp); message.saveChanges(); // // 将创建的Email写入到本地磁盘存储 // message.writeTo(new FileOutputStream("E:\\attachMail.eml"));
return message; } } |
四、发送一封正文含有图片且附带附件的邮件
package com.zzs.commons.mail;
import java.util.Properties;
import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.Message; import javax.mail.Session; import javax.mail.Transport; 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 MailTest4 { public static void main(String[] args) throws Exception {
Properties prop = new Properties(); prop.setProperty("mail.host", "smtp.sohu.com"); // 设置邮件服务器主机名 prop.setProperty("mail.transport.protocol", "smtp"); // 发送邮件协议名称 prop.setProperty("mail.smtp.auth", "true");// 发送服务器需要身份验证
// 使用JavaMail发送邮件的5个步骤 // 1、创建session Session session = Session.getInstance(prop); session.setDebug(true); // 2、通过session得到transport对象 Transport ts = session.getTransport(); ts.connect("smtp.sohu.com", "用户名", "密码"); // 4、创建邮件 Message message = createMixedMail(session); // 5、发送邮件 ts.sendMessage(message, message.getAllRecipients()); ts.close(); }
/** * 生成一封带附件和带图片的邮件 */ public static MimeMessage createMixedMail(Session session) throws Exception { // 创建邮件 MimeMessage message = new MimeMessage(session);
// 设置邮件的基本信息 message.setFrom(new InternetAddress("xxx@sohu.com")); message.setRecipient(Message.RecipientType.TO, new InternetAddress("xxx@163.com")); message.setSubject("带附件和带图片的的邮件");
// 正文 MimeBodyPart text = new MimeBodyPart(); text.setContent("这里是正文<br/>这里有<img src='cid:001.jpg'>图片。", "text/html;charset=UTF-8");
// 正文图片 MimeBodyPart image = new MimeBodyPart(); image.setDataHandler(new DataHandler(new FileDataSource("E:\\test\\data\\mail\\123.jpg"))); image.setContentID("001.jpg");
// 描述关系:正文和图片 MimeMultipart mp1 = new MimeMultipart(); mp1.addBodyPart(text); mp1.addBodyPart(image); mp1.setSubType("related");
// 代表正文的bodypart MimeBodyPart content = new MimeBodyPart(); content.setContent(mp1);
// 附件1 MimeBodyPart attach = new MimeBodyPart(); DataHandler dh = new DataHandler(new FileDataSource("E:\\test\\data\\mail\\456.zip")); attach.setDataHandler(dh); attach.setFileName(dh.getName());
// 附件2 MimeBodyPart attach2 = new MimeBodyPart(); DataHandler dh2 = new DataHandler(new FileDataSource("E:\\test\\data\\mail\\文档.rar")); attach2.setDataHandler(dh2); attach2.setFileName(MimeUtility.encodeText(dh2.getName()));
// 描述关系:正文和附件 MimeMultipart mp2 = new MimeMultipart(); mp2.addBodyPart(content); mp2.addBodyPart(attach); mp2.addBodyPart(attach2); mp2.setSubType("mixed");
message.setContent(mp2); message.saveChanges();
return message; } } |
参考文档:
http://www.cnblogs.com/xdp-gacl/p/4216311.html