Spring Boot 2.X 发送模板邮件

admin 5月前 3511

经常能看到各种花里胡哨的邮件内容,这是怎么实现的呢?

1:导入依赖

mail 包是 Spring 封装的 mail 邮件包

 <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

2:配置邮箱信息

spring:
  mail:
    protocol: smtp
    port: 465
    host: smtpdm.aliyun.com
    username: service@mail.nextrt.com
    password: password
    default-encoding: UTF-8
    properties:
      mail:
        smtp:
          auth: true
          starttls:
            enable: true
            required: true

修改上面port,host,username,password等信息,现在大部分主机商都屏蔽了25端口,所以推荐使用TLS加密的465端口

3:编写邮件模版

<body style="color: #010e07; font-size: 14px; " xmlns:th="http://www.w3.org/1999/xhtml">
  <div style="width: 80%; margin: 20px auto; max-width: 800px; min-width: 600px;">
    <div style="padding: 15px 25px;background-color: #66CCFF; border-top-left-radius: 10px;border-top-right-radius: 10px;color: #fff;overflow: hidden;line-height: 32px;">
      <div style="color:#ffffff"><h3 th:text="${title}"></h3></div>
    </div>
    <div style="border-bottom-left-radius: 10px;border-bottom-right-radius: 10px;border:1px solid #ddd;overflow: hidden;padding: 15px 15px 20px;background-color: #26de81;">
      <div style="padding:15px;"><strong style="margin: 10px 0;" th:text="${content}"></strong></div>
      <br>
      <div style="text-align: right; color: #111111; padding: 0 15px 15px;">GeekNotice</div>
    </div>
  </div>
</body>

模版可以自己编写

4:编写Service

@Service
public class EmailServiceImpl implements EmailService {

    //模板引擎对象
    @Autowired
    private TemplateEngine templateEngine;

    @Autowired
    private JavaMailSender javaMailSender;

    //发送邮件的用户名
    @Value("${spring.mail.username}")
    private String sender;


    public static String createTemplates(Map<String, Object> dataMap, String TemplatesName, TemplateEngine templateEngine) {
        Context context = new Context();
        context.setVariables(dataMap);
        String emailText = templateEngine.process(TemplatesName, context);
        System.out.println(emailText);
        return emailText;
    }

    @Override
    public int EmailSend(String Email, String SubTitle, String Content) {
        //消息处理类
        MimeMessage message = javaMailSender.createMimeMessage();
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("title", SubTitle);
        dataMap.put("content", Content);
        //获取生成的模板
        String emailText = createTemplates(dataMap, "email.html", templateEngine);
        try {
            //消息处理助手对象
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            //设置发件人
            helper.setFrom(sender);
            //设置收件人
            helper.setTo(Email);
            //设置邮件标题
            helper.setSubject(SubTitle + "-GeekNoticet");
            //设置邮件内容 ,true 表示发送html 格式
            helper.setText(emailText, true);
        } catch (MessagingException e) {
            throw new RuntimeException("Messaging  Exception !", e);
        }
        javaMailSender.send(message);
        return 1;
    }
}

调用方法测试即可

最新回复 (0)
全部楼主
返回
发新帖