概述
以QQ邮箱示例
<!-- https://mvnrepository.com/artifact/javax.mail/javax.mail-api -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>javax.mail-api</artifactId>
<version>1.6.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.mail/mail -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
- 发送协议SMTP
- 接收协议POP3
发送文本邮件
@Test
public void testMail() throws Exception {
Properties prop = new Properties();
prop.setProperty("mail.host", "smtp.qq.com"); // 邮件服务器
prop.setProperty("mail.transport.protocol", "smtp"); // 邮件协议
prop.setProperty("mail.smtp.auth", "true"); // 是否验证用户名密码
// SSL加密
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
prop.setProperty("mail.smtp.ssl.enable", "true");
prop.put("mail.smtp.ssl.socketFactory", sf);
// 1.创建应用程序环境信息session对象
Session session = Session.getDefaultInstance(prop, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
// qq邮箱用户名,授权码
return new PasswordAuthentication("xxx@qq.com", "mhcxadh");
}
});
session.setDebug(true); // 开启debug模式
// 2.通过session得到transport对象
Transport ts = session.getTransport();
// 3.使用邮箱的用户名和授权码连上邮箱服务器
ts.connect("smtp.qq.com", "xxx@qq.com", "mcadh");
// 4.创建邮件
// 4.1 创建邮件对象
MimeMessage message = new MimeMessage(session);
// 4.2 指定发件人
message.setFrom(new InternetAddress("xxx@qq.com"));
// 4.3 指明发件人
message.setRecipient(Message.RecipientType.TO, new InternetAddress("xxx@icloud.com"));
// 4.4 邮件的标题
message.setSubject("邮件标题测试");
// 4.5 邮件正文
message.setContent("你好", "text/html;charset=utf-8");
// 5.发送邮件
ts.sendMessage(message, message.getAllRecipients());
// 6.关闭连接
ts.close();
}
正文嵌入图片
@Test
public void testMail() throws Exception {
Properties prop = new Properties();
prop.setProperty("mail.host", "smtp.qq.com"); // 邮件服务器
prop.setProperty("mail.transport.protocol", "smtp"); // 邮件协议
prop.setProperty("mail.smtp.auth", "true"); // 是否验证用户名密码
// SSL加密
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
prop.setProperty("mail.smtp.ssl.enable", "true");
prop.put("mail.smtp.ssl.socketFactory", sf);
// 1.创建应用程序环境信息session对象
Session session = Session.getDefaultInstance(prop, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
// qq邮箱用户名,授权码
return new PasswordAuthentication("xxxxx@qq.com", "mdh");
}
});
session.setDebug(true);
// 2.通过session得到transport对象
Transport ts = session.getTransport();
// 3.使用邮箱的用户名和授权码连上邮箱服务器
ts.connect("smtp.qq.com", "xxxxx@qq.com", "mhdh");
// 4.创建邮件
// 4.1 创建邮件对象
MimeMessage message = new MimeMessage(session);
// 4.2 指定发件人
message.setFrom(new InternetAddress("xxxxx@qq.com"));
// 4.3 指明发件人
message.setRecipient(Message.RecipientType.TO, new InternetAddress("xxxxx@icloud.com"));
// 4.4 邮件的标题
message.setSubject("邮件标题测试");
// ========================================================
// 准备图片数据
// target/Servlet/WEB-INF/classes/动漫头像.jpg
MimeBodyPart image = new MimeBodyPart();
DataHandler dh = new DataHandler(new FileDataSource("/Users/collin/IdeaProjects/Kuang/JavaWeb02-muti-maven/Servlet/target/Servlet/WEB-INF/classes/动漫头像.jpg"));
image.setDataHandler(dh);
image.setContentID("dm.jpg");
// 准备邮件正文
MimeBodyPart text = new MimeBodyPart();
text.setContent("带图片邮件<img src='cid:dm.jpg'>", "text/html;charset=utf-8");
// 描述数据关系
MimeMultipart mm = new MimeMultipart();
mm.addBodyPart(text);
mm.addBodyPart(image);
mm.setSubType("related");
// 保存到消息中
message.setContent(mm);
message.saveChanges();
// ========================================================
// 5.发送邮件
ts.sendMessage(message, message.getAllRecipients());
// 6.关闭连接
ts.close();
}
附件
@Test
public void testMail() throws Exception {
Properties prop = new Properties();
prop.setProperty("mail.host", "smtp.qq.com"); // 邮件服务器
prop.setProperty("mail.transport.protocol", "smtp"); // 邮件协议
prop.setProperty("mail.smtp.auth", "true"); // 是否验证用户名密码
// SSL加密
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
prop.setProperty("mail.smtp.ssl.enable", "true");
prop.put("mail.smtp.ssl.socketFactory", sf);
// 1.创建应用程序环境信息session对象
Session session = Session.getDefaultInstance(prop, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
// qq邮箱用户名,授权码
return new PasswordAuthentication("xxxxx@qq.com", "mhdh");
}
});
session.setDebug(true);
// 2.通过session得到transport对象
Transport ts = session.getTransport();
// 3.使用邮箱的用户名和授权码连上邮箱服务器
ts.connect("smtp.qq.com", "xxxxx@qq.com", "mhcadh");
// 4.创建邮件
// 4.1 创建邮件对象
MimeMessage message = new MimeMessage(session);
// 4.2 指定发件人
message.setFrom(new InternetAddress("xxxxx@qq.com"));
// 4.3 指明发件人
message.setRecipient(Message.RecipientType.TO, new InternetAddress("xxxxx@icloud.com"));
// 4.4 邮件的标题
message.setSubject("邮件标题测试");
// ========================================================
// 准备图片数据
// target/Servlet/WEB-INF/classes/动漫头像.jpg
MimeBodyPart image = new MimeBodyPart();
DataHandler dh = new DataHandler(new FileDataSource("/Users/collin/IdeaProjects/Kuang/JavaWeb02-muti-maven/Servlet/target/Servlet/WEB-INF/classes/动漫头像.jpg"));
image.setDataHandler(dh);
image.setContentID("dm.jpg");
// 准备邮件正文
MimeBodyPart text = new MimeBodyPart();
text.setContent("带图片邮件<img src='cid:dm.jpg'>", "text/html;charset=utf-8");
// 准备附件1
MimeBodyPart attachFile1 = new MimeBodyPart();
attachFile1.setDataHandler(new DataHandler(new FileDataSource("/Users/collin/IdeaProjects/Kuang/JavaWeb02-muti-maven/Servlet/target/Servlet/WEB-INF/classes/动漫头像.jpg")));
attachFile1.setFileName("图片1.jpg");
// 准备附件2
MimeBodyPart attachFile2 = new MimeBodyPart();
attachFile2.setDataHandler(new DataHandler(new FileDataSource("/Users/collin/IdeaProjects/Kuang/JavaWeb02-muti-maven/Servlet/target/Servlet/WEB-INF/classes/动漫头像.jpg")));
attachFile2.setFileName("图片2.jpg");
// 拼接正文数据关系
MimeMultipart mm = new MimeMultipart();
mm.addBodyPart(text);
mm.addBodyPart(image);
mm.setSubType("related");
MimeBodyPart contextText = new MimeBodyPart();
contextText.setContent(mm);
// 拼接附件
MimeMultipart allFile = new MimeMultipart();
allFile.addBodyPart(attachFile1);
allFile.addBodyPart(attachFile2);
allFile.addBodyPart(contextText);
allFile.setSubType("mixed");
// 保存到消息中
message.setContent(allFile);
message.saveChanges();
// ========================================================
// 5.发送邮件
ts.sendMessage(message, message.getAllRecipients());
// 6.关闭连接
ts.close();
}