Spring + Thymeleaf 发送邮件

1 Pom等未展示的内容

可见此处

2 EmailService 处理邮件逻辑

@Service
public class EmailService {


    private TemplateEngine emailTemplateEngine;

    public EmailService(SpringTemplateEngine emailTemplateEngine) {
        this.emailTemplateEngine = emailTemplateEngine;
    }

    public void sendEmail(String recipientName, String recipientEmail) throws MessagingException {
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        mailSender.setUsername("example@qq.com");
        // qq独立密码
        mailSender.setPassword("---");
        mailSender.setHost("smtp.qq.com");
        Properties props = new Properties();
        props.put("mail.smtp.auth", true);
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.port", 587);
        // props.put("mail.smtp.ssl.trust", "smtp.gmail.com");
        props.put("mail.smtp.ssl.protocols", "TLSv1.2");
        props.put("mail.smtp.connectiontimeout", 20000);
        mailSender.setJavaMailProperties(props);

        MimeMessage mimeMessage = mailSender.createMimeMessage();
        // 此处不加UTF-8,邮件里的中文会乱码
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, "UTF-8");
        helper.setSubject("Test email");
        helper.setFrom("example@qq.com");
        helper.setTo(recipientEmail);

        Context ctx = new Context();
        ctx.setVariable("name", recipientName);
        /** 此处路径 “email-template” 被模板引擎(SpringTemplateEngine)里的
        模板解析器(SpringResourceTemplateResolver)解析后文件的路径为
        “classpath:templates/email-template.html” */
        String processedText = emailTemplateEngine.process("email-template", ctx);
		
		// true = 邮件为html格式
        helper.setText(processedText, true);

        mailSender.send(mimeMessage);

    }
}

3 SpringMVC.xml 配置thymeleaf模板引擎

该模板引擎用于将html文件解析为String

<!--    /* ******************************************************************** */-->
<!--    /*  THYMELEAF-SPECIFIC ARTIFACTS FOR EMAIL                              */-->
<!--    /*  TemplateResolver(3) <- TemplateEngine                               */-->
<!--    /* ******************************************************************** */-->
    <bean id="emailTemplateEngine" class="org.thymeleaf.spring5.SpringTemplateEngine">
        <property name="templateResolver">
            <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
                <property name="templateMode" value="HTML"/>
                <property name="prefix" value="classpath:templates/"/>
                <property name="suffix" value=".html"/>
                <property name="characterEncoding" value="UTF-8"/>
                <!--thymeleaf会默认开启页面缓存,导致我们修改页面不会立即展现效果,在这里关闭-->
                <property name="cacheable" value="false"/>
            </bean>
        </property>
    </bean>

4 用于生成String的模板HTML

<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <title th:remove="all">Template for HTML email with inline image</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>

<p th:text="'你好'+${name}"></p>
<p> 我是谁?</p>

<p>
    Regards, <br />
    <em>The Thymeleaf Team</em>
</p>
</body>
</html>

5 工程目录

在这里插入图片描述

路径的一些问题

  • TemplateResolver的prefix
    1. 加classpath:thymeleaf会到resources文件夹下找文件
    2. 不加classpath:thymeleaf会到webapp文件夹下找文件
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值