Spring Boot(二十一):发送邮件(多账号轮询发送)

介绍

pom文件引入:

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>

配置:

1
2
3
4
spring.mail.host = 
spring.mail.username =
spring.mail.password =
spring.mail.properties.mail.smtp.auth = true

多账号实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
@Configuration
@EnableConfigurationProperties(MailProperties.class)
public class DoddJavaMailSenderImpl extends JavaMailSenderImpl implements JavaMailSender {

private ArrayList<String> usernameList;
private ArrayList<String> passwordList;
private int currentMailId = 0;

private final MailProperties properties;

public DoddJavaMailSenderImpl(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);
}

}

实现类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
@Component
public class DoddJavaMailComponent {
private static final String template = "mail/dodd.ftl";

@Autowired
private FreeMarkerConfigurer freeMarkerConfigurer;
@Autowired
private DoddJavaMailSenderImpl 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("Dodd", "UTF-8");
helper.setFrom(from);
helper.setTo(email);
helper.setSubject("测试邮件");
helper.setText(text, true);
javaMailSender.send(message);
return text;
}

}

controller:

1
2
3
4
5
6
7
8
9
10
11
12
13
@RestController
@RequestMapping("/api")
public class MailController {

@Autowired
private DoddJavaMailComponent component;

@RequestMapping(value = "mail")
public String mail(String email) {
component.sendMail(email);
return "success";
}
}