1.pom.xml 依赖

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

    2.配置属性

    1. # mail
    2. spring.mail.host:smtp.exmail.qq.com
    3. spring.mail.username:fengyw@roncoo.com,service@roncoo.com,education@roncoo.com
    4. spring.mail.password:123
    5. spring.mail.properties.mail.smtp.auth:true

    3.配置类

    实现多账号轮询发送邮件

    1. /**
    2. * 实现多账号,轮询发送
    3. */
    4. @Configuration
    5. @EnableConfigurationProperties(MailProperties.class)
    6. public class RoncooJavaMailSenderImpl extends JavaMailSenderImpl implements JavaMailSender {
    7. private ArrayList<String> usernameList;
    8. private ArrayList<String> passwordList;
    9. private int currentMailId = 0;
    10. private final MailProperties properties;
    11. public RoncooJavaMailSenderImpl(MailProperties properties) {
    12. this.properties = properties;
    13. // 初始化账号
    14. if (usernameList == null)
    15. usernameList = new ArrayList<String>();
    16. String[] userNames = this.properties.getUsername().split(",");
    17. if (userNames != null) {
    18. for (String user : userNames) {
    19. usernameList.add(user);
    20. }
    21. }
    22. // 初始化密码
    23. if (passwordList == null)
    24. passwordList = new ArrayList<String>();
    25. String[] passwords = this.properties.getPassword().split(",");
    26. if (passwords != null) {
    27. for (String pw : passwords) {
    28. passwordList.add(pw);
    29. }
    30. }
    31. }
    32. @Override
    33. protected void doSend(MimeMessage[] mimeMessage, Object[] object) throws MailException {
    34. super.setUsername(usernameList.get(currentMailId));
    35. super.setPassword(passwordList.get(currentMailId));
    36. // 设置编码和各种参数
    37. super.setHost(this.properties.getHost());
    38. super.setDefaultEncoding(this.properties.getDefaultEncoding().name());
    39. super.setJavaMailProperties(asProperties(this.properties.getProperties()));
    40. super.doSend(mimeMessage, object);
    41. // 轮询
    42. currentMailId = (currentMailId + 1) % usernameList.size();
    43. }
    44. private Properties asProperties(Map<String, String> source) {
    45. Properties properties = new Properties();
    46. properties.putAll(source);
    47. return properties;
    48. }
    49. @Override
    50. public String getUsername() {
    51. return usernameList.get(currentMailId);
    52. }
    53. }

    4.发送邮件类

    1. /**
    2. * 实现发送功能
    3. */
    4. @Component
    5. public class RoncooJavaMailComponent {
    6. private static final String template = "mail/roncoo.ftl";
    7. @Autowired
    8. private FreeMarkerConfigurer freeMarkerConfigurer;
    9. @Autowired
    10. private RoncooJavaMailSenderImpl javaMailSender;
    11. public void sendMail(String email) {
    12. Map<String, Object> map = new HashMap<String, Object>();
    13. map.put("email", email);
    14. try {
    15. String text = getTextByTemplate(template, map);
    16. send(email, text);
    17. } catch (IOException | TemplateException e) {
    18. e.printStackTrace();
    19. } catch (MessagingException e) {
    20. e.printStackTrace();
    21. }
    22. }
    23. private String getTextByTemplate(String template, Map<String, Object> model) throws TemplateNotFoundException,
    24. MalformedTemplateNameException, ParseException, IOException, TemplateException {
    25. return FreeMarkerTemplateUtils
    26. .processTemplateIntoString(freeMarkerConfigurer.getConfiguration().getTemplate(template), model);
    27. }
    28. private String send(String email, String text) throws MessagingException, UnsupportedEncodingException {
    29. MimeMessage message = javaMailSender.createMimeMessage();
    30. MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
    31. InternetAddress from = new InternetAddress();
    32. from.setAddress(javaMailSender.getUsername());
    33. from.setPersonal("龙果学院", "UTF-8");
    34. helper.setFrom(from);
    35. helper.setTo(email);
    36. helper.setSubject("测试邮件");
    37. helper.setText(text, true);
    38. javaMailSender.send(message);
    39. return text;
    40. }
    41. }

    5.FreeMarker 模板

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    5. </head>
    6. <body>
    7. <div style="width: 600px; text-align: left; margin: 0 auto;">
    8. <h1 style="color: #005da7;">龙果学院</h1>
    9. <div style="border-bottom: 5px solid #005da7; height: 2px; width: 100%;"></div>
    10. <div style="border: 1px solid #005da7; font-size: 16px; line-height: 50px; padding: 20px;">
    11. <div>${email},您好!</div>
    12. <div>
    13. 这是个测试
    14. </div>
    15. <div style="border-bottom: 2px solid #005da7; height: 2px; width: 100%;"></div>
    16. <div>扫一扫,关注龙果学院微信公共号,里面更多精彩推荐</div>
    17. <div>
    18. <img src="http://account.roncoo.com/images/qrcode.png" alt="龙果学院公众号二维码" />
    19. </div>
    20. <div>
    21. 想了解更多信息,请访问
    22. <a href="http://www.roncoo.com">http://www.roncoo.com</a>
    23. </div>
    24. </div>
    25. </div>
    26. </body>
    27. </html>