• 邮箱控制
      • 启用客户端 SMTP服务
    • Spring Email

      • 导入依赖的 jar 包

        1. <!-- 导入发邮件的依赖 -->
        2. <dependency>
        3. <groupId>org.springframework.boot</groupId>
        4. <artifactId>spring-boot-starter-mail</artifactId>
        5. <version>2.6.2</version>
        6. </dependency>
      • 邮箱参数设置

        1. # MailProperties
        2. spring.mail.host=smtp.sina.com
        3. spring.mail.username=xxx@xxx.com
        4. spring.mail.password=xxxxxx
        5. spring.mail.protocol=smtp
        6. spring.mail.properties.mail.smtp.ssl.enable=true
      • 使用 JavaMailSender 发送邮件:封装一个工具类MailClient ```java import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.stereotype.Component;

    import javax.mail.MessagingException; import javax.mail.internet.MimeMessage;

    @Component public class MailClient { private static final Logger logger = LoggerFactory.getLogger(MailClient.class);

    1. @Autowired
    2. private JavaMailSender mailSender;
    3. @Value("${spring.mail.username}")
    4. private String from;
    5. public void sendMail(String to,String subject,String content) {
    6. try {
    7. MimeMessage message = mailSender.createMimeMessage();
    8. MimeMessageHelper helper = new MimeMessageHelper(message);
    9. helper.setFrom(from);
    10. helper.setTo(to);
    11. helper.setSubject(subject);
    12. helper.setText(content,true);
    13. mailSender.send(helper.getMimeMessage());
    14. } catch (MessagingException e) {
    15. logger.error("发送邮件失败:"+e.getMessage());
    16. }
    17. }

    }

    1. - 模板引擎
    2. - 使用 Thymeleaf 发送 HTML 邮件
    3. ```html
    4. <!DOCTYPE html>
    5. <html lang="en" xmlns:th="http://www.thymeleaf.org">
    6. <head>
    7. <meta charset="UTF-8">
    8. <title>邮件示例</title>
    9. </head>
    10. <body>
    11. <p>欢迎你,<span th:text="${username}"></span></p>
    12. </body>
    13. </html>
    • 测试使用相应的HTML模板发送邮件

      1. @RunWith(SpringRunner.class)
      2. @ContextConfiguration(classes = CommunityApplication.class)
      3. @SpringBootTest
      4. public class MailTests {
      5. // 注入已经封装好的工具类
      6. @Autowired
      7. private MailClient mailClient;
      8. // 注入模板引擎
      9. @Autowired
      10. private TemplateEngine templateEngine;
      11. @Test
      12. public void testSendMail() {
      13. mailClient.sendMail("2218877219@qq.com","SpringBootTest","hello");
      14. }
      15. @Test
      16. public void testHtmlMail() {
      17. Context context = new Context();
      18. context.setVariable("username","Ethereal");
      19. // 使用 resources/templates/mail/demo.html作为模板发送邮件
      20. String content = templateEngine.process("/mail/demo",context);
      21. System.out.println(content);
      22. mailClient.sendMail("2218877219@qq.com","HtmlMailTest","welcome");
      23. }
      24. }