SpringBoot2 - JavaMail
2018-06-21 本文已影响14人
朱穆朗玛
构建项目

添加依赖
http://mvnrepository.com/artifact/javax.mail/mail/1.4.7
修改pom.xml
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
封装JavaMail
- 编写MailEntity类用来保存发送邮件时所需参数
package com.gala.mail.entity;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
public class MailEntity implements Serializable {
private static final long serialVersionUID = 3224071517369692657L;
private String smtpService;
private String smtpPort;
private String fromMailAdress;
private String fromMailStmpPwd;
private String title;
private String content;
private String contentType;
private List<String> list = new ArrayList<>();
public String getSmtpService() {
return smtpService;
}
public void setSmtpService(String smtpService) {
this.smtpService = smtpService;
}
public String getSmtpPort() {
return smtpPort;
}
public void setSmtpPort(String smtpPort) {
this.smtpPort = smtpPort;
}
public String getFromMailAdress() {
return fromMailAdress;
}
public void setFromMailAdress(String fromMailAdress) {
this.fromMailAdress = fromMailAdress;
}
public String getFromMailStmpPwd() {
return fromMailStmpPwd;
}
public void setFromMailStmpPwd(String fromMailStmpPwd) {
this.fromMailStmpPwd = fromMailStmpPwd;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getContentType() {
return contentType;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
}
- 编写MailSender,它是邮件发送者实体,用来配置发送邮件参数以及执行发送邮件
2.1 新建邮件内容枚举类MailContentTypeEnum
package com.gala.mail.util;
public enum MailContentTypeEnum {
HTML("text/html;charset=UTF-8"), TEXT("text");
private String value;
MailContentTypeEnum(String value) {
this.value = value;
}
public String getValue() {
return value;
}
}
2.2 新建配置类
新建PropertiesUtil工具类用于读取*.properties配置文件
package com.gala.mail.util;
import java.util.Locale;
import java.util.ResourceBundle;
public class PropertiesUtil {
private final ResourceBundle resource;
private final String fileName;
/**
* 获取文件资源对象
*/
public PropertiesUtil(String fileName) {
this.fileName = fileName;
Locale locale = new Locale("zh", "CN");
this.resource = ResourceBundle.getBundle(this.fileName, locale);
}
/**
* 根据key获取value
*/
public String getValue(String key) {
String message = this.resource.getString(key);
return message;
}
}
在/resource目录下创建mail.properties配置文件
#发送服务器的smtp地址
mail.smtp.service=smtp.qq.com
#发送氱的smtp端口号
mail.smtp.port=587
#发件人
mail.from.address=76345045@qq.com
#smtp授权密码
mail.from.smtp.pwd=xxx
#发件人昵称
mail.from.nickname=旮旯
2.3 MailSender
package com.gala.mail.util;
import java.util.List;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
import com.gala.mail.entity.MailEntity;
public class MailSender {
// 邮件实体
private static MailEntity mail = new MailEntity();
/**
* 设置邮件标题
*/
public MailSender title(String title) {
mail.setTitle(title);
return this;
}
/**
* 设置邮件内容
*/
public MailSender content(String content) {
mail.setContent(content);
return this;
}
/**
* 设置邮件格式
*/
public MailSender contentType(MailContentTypeEnum typeEnum) {
mail.setContentType(typeEnum.getValue());
return this;
}
/**
* 设置邮件请求目标地址
*/
public MailSender targets(List<String> targets) {
mail.setList(targets);
return this;
}
/**
* 发送邮件
*/
public void send() throws Exception {
// 默认使用html内容发送
if (mail.getContentType() == null) {
mail.setContentType(MailContentTypeEnum.HTML.getValue());
}
if (mail.getTitle() == null || mail.getTitle().trim().length() == 0) {
throw new Exception("没有设置邮件标题");
}
if (mail.getContent() == null || mail.getContent().trim().length() == 0) {
throw new Exception("没有设置邮件内容");
}
if (mail.getList().size() == 0) {
throw new Exception("没有设置接收者");
}
// 读取resource目录下mail.properties
final PropertiesUtil pu = new PropertiesUtil("mail");
// 记录邮箱属性
final Properties props = new Properties();
// 身份验证
props.put("mail.smtp.auth", "true");
// smtp服务器
props.put("mail.smtp.host", pu.getValue("mail.smtp.service"));
// smtp端口号
props.put("mail.smtp.port", pu.getValue("mail.smtp.port"));
// 发件人
props.put("mail.user", pu.getValue("mail.from.address"));
// 16位口令
props.put("mail.password", pu.getValue("mail.from.smtp.pwd"));
// 构建授权信息,进行smtp身份验证
Authenticator authenticator = new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
// 用户名 密码
String userName = props.getProperty("mail.user");
String password = props.getProperty("mail.password");
return new PasswordAuthentication(userName, password);
}
};
// 创建邮件会话
Session mailSession = Session.getInstance(props, authenticator);
// 创建邮件消息
MimeMessage message = new MimeMessage(mailSession);
// 设置发件人
String nickName = MimeUtility.encodeText(pu.getValue("mail.from.nickname"));
InternetAddress from = new InternetAddress(nickName + "<" + props.getProperty("mail.user") + ">");
message.setFrom(from);
// 设置邮件标题
message.setSubject(mail.getTitle());
if (mail.getContentType().endsWith(MailContentTypeEnum.HTML.getValue())) {
// html邮件
message.setContent(mail.getContent(), mail.getContentType());
} else if (mail.getContentType().equals(MailContentTypeEnum.TEXT.getValue())) {
// 文本邮件
message.setText(mail.getContent());
}
// 发送邮箱地址
List<String> targets = mail.getList();
for (int i = 0; i < targets.size(); i++) {
try {
// 设置收件人
InternetAddress to = new InternetAddress(targets.get(i));
message.setRecipient(Message.RecipientType.TO, to);
// 发送
Transport.send(message);
} catch (Exception e) {
continue;
}
}
}
}
开启POP3/SMTP


测试发送邮件
新建TestMail类
package com.gala.mail.util;
import java.util.ArrayList;
public class TestMail {
public static void main(String[] args) throws Exception {
new MailSender()
.title("测试SpringBoot发送邮件")
.content("测试SpringBoot发送邮件内容")
.contentType(MailContentTypeEnum.TEXT)
.targets(new ArrayList<String>(){{
add("zhuqx1130@163.com");
}})
.send();
}
}