1.pom.xml 依赖
<!-- mail -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
2.配置属性
spring.mail.host:smtp.exmail.qq.com
spring.mail.username:fengyw@roncoo.com,service@roncoo.com,education@roncoo.com
spring.mail.password:123
spring.mail.properties.mail.smtp.auth:true
3.配置类
实现多账号轮询发送邮件
/**
* 实现多账号,轮询发送
*/
@Configuration
@EnableConfigurationProperties(MailProperties.class)
public class RoncooJavaMailSenderImpl extends JavaMailSenderImpl implements JavaMailSender {
private ArrayList<String> usernameList;
private ArrayList<String> passwordList;
private int currentMailId = 0;
private final MailProperties properties;
public RoncooJavaMailSenderImpl(MailProperties properties) {
this.properties = properties;
// 初始化账号
if (usernameList == null)
usernameList = new ArrayList<String>();
String[] userNames = this.properties.getUsername().split(",");
if (userNames != null) {
for (String user : userNames) {
usernameList.add(user);
}
}
// 初始化密码
if (passwordList == null)
passwordList = new ArrayList<String>();
String[] passwords = this.properties.getPassword().split(",");
if (passwords != null) {
for (String pw : passwords) {
passwordList.add(pw);
}
}
}
@Override
protected void doSend(MimeMessage[] mimeMessage, Object[] object) throws MailException {
super.setUsername(usernameList.get(currentMailId));
super.setPassword(passwordList.get(currentMailId));
// 设置编码和各种参数
super.setHost(this.properties.getHost());
super.setDefaultEncoding(this.properties.getDefaultEncoding().name());
super.setJavaMailProperties(asProperties(this.properties.getProperties()));
super.doSend(mimeMessage, object);
// 轮询
currentMailId = (currentMailId + 1) % usernameList.size();
}
private Properties asProperties(Map<String, String> source) {
Properties properties = new Properties();
properties.putAll(source);
return properties;
}
@Override
public String getUsername() {
return usernameList.get(currentMailId);
}
}
4.发送邮件类
/**
* 实现发送功能
*/
@Component
public class RoncooJavaMailComponent {
private static final String template = "mail/roncoo.ftl";
@Autowired
private FreeMarkerConfigurer freeMarkerConfigurer;
@Autowired
private RoncooJavaMailSenderImpl javaMailSender;
public void sendMail(String email) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("email", email);
try {
String text = getTextByTemplate(template, map);
send(email, text);
} catch (IOException | TemplateException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
}
private String getTextByTemplate(String template, Map<String, Object> model) throws TemplateNotFoundException,
MalformedTemplateNameException, ParseException, IOException, TemplateException {
return FreeMarkerTemplateUtils
.processTemplateIntoString(freeMarkerConfigurer.getConfiguration().getTemplate(template), model);
}
private String send(String email, String text) throws MessagingException, UnsupportedEncodingException {
MimeMessage message = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
InternetAddress from = new InternetAddress();
from.setAddress(javaMailSender.getUsername());
from.setPersonal("龙果学院", "UTF-8");
helper.setFrom(from);
helper.setTo(email);
helper.setSubject("测试邮件");
helper.setText(text, true);
javaMailSender.send(message);
return text;
}
}
5.FreeMarker 模板
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<div style="width: 600px; text-align: left; margin: 0 auto;">
<h1 style="color: #005da7;">龙果学院</h1>
<div style="border-bottom: 5px solid #005da7; height: 2px; width: 100%;"></div>
<div style="border: 1px solid #005da7; font-size: 16px; line-height: 50px; padding: 20px;">
<div>${email},您好!</div>
<div>
这是个测试
</div>
<div style="border-bottom: 2px solid #005da7; height: 2px; width: 100%;"></div>
<div>扫一扫,关注龙果学院微信公共号,里面更多精彩推荐</div>
<div>
<img src="http://account.roncoo.com/images/qrcode.png" alt="龙果学院公众号二维码" />
</div>
<div>
想了解更多信息,请访问
<a href="http://www.roncoo.com">http://www.roncoo.com</a>
</div>
</div>
</div>
</body>
</html>