之前寫了兩篇關於 Spring Boot 發送郵件的博客,一篇是簡單的,包括搭建環境、依賴等,一篇是功能更豐富,包括如何發送附件郵件和內容帶圖片的郵件,有興趣可以看一下
Spring Boot發送郵件一
Spring Boot發送郵件二
下邊這篇就開始展示如何利用前端模板 Thymeleaf 發送郵件
Thymeleaf做模板發送郵件
- 1.在pom.xml中加入 Thymeleaf 的依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
- 2.在
src/resources/templates
創建 mail.html 文件
這裏直接上模板代碼,需要注意的是創建 HTML 後導入Thymeleaf 的網址<html lang="en" xmlns:th="http://www.themeleaf.org">
,然後就可以使用 Thymeleaf的標記符號th:xxx1 = ${xxx2}
了; xxx1 代表操作方式,比如th:text
代表文本,th:if
代表條件表達式;xxx2代表後端提供的屬性;還有要用${}
包裹後端提供的屬性名稱 - 3. HTML 代碼:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.themeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div>
歡迎<span th:text=${username}></span>加入xxx大家庭
</div>
您的入職信息如下
<table border="1">
<tr>
<td>職位</td>
<td th:text=${position}></td>
</tr>
<tr>
<td>職稱</td>
<td th:text=${joblevel}></td>
</tr>
<tr>
<td>薪水</td>
<td th:text=${salary}></td>
</tr>
<tr>
<td>部門</td>
<td th:text=${dept}></td>
</tr>
</table>
<div style="color: #ff5d9b;font-size: 20px;" >
希望在未來日子裏,攜手共進
</div>
</body>
</html>
- 4.後端代碼
@SpringBootTest
class MailApplicationTests {
@Autowired
JavaMailSender javaMailSender;//郵件發送
@Autowired
SpringTemplateEngine springTemplateEngine;//Spring 模板引擎
@Test
public void test4() throws MessagingException {
MimeMessage msg = javaMailSender.createMimeMessage();//創建模擬的消息
MimeMessageHelper helper = new MimeMessageHelper(msg, true);//創建模擬的消息工具
helper.setFrom("[email protected]");//發件人
helper.setSentDate(new Date());//發送日期
helper.setSubject("這是測試主題(thymeleaf)");//發送主題
Context context = new Context();
context.setVariable("username","javaboy");
context.setVariable("position","Java工程師");
context.setVariable("dept","產品研發部");
context.setVariable("salary",9999);
context.setVariable("joblevel","高級工程師");
String process = springTemplateEngine.process("mail.html", context);
helper.setText(process,true);//內容是否設置成html,true代表是
helper.setTo("[email protected]");//收件人
javaMailSender.send(msg);//發送
}
與之前的發送內容有圖片的郵件相比,步驟幾乎是一樣的,只不過發送內容多 Spring Boot提供的模板引擎SpringTemplateEngine.process()
來生成字符串內容的步驟,然後再放入MimeMessageHelper.setText()
方法中;還有一個不同就是Context
這個類來處理後端屬性,它是在 Thymeleaf 包下,導入Thymeleaf 依賴後出現的,功能很簡單,不用多說了;下面着重看一下 SpringTemplateEngine
這個類,它是一個模板引擎,由 Spring Boot自動化配置提供的,減少了很多配置,目前用它處理 Context
來生成 HTML 的字符串。源碼在 ThymeleafAutoConfiguration
中自動注入:
@Bean
@ConditionalOnMissingBean({ISpringTemplateEngine.class})
SpringTemplateEngine templateEngine(ThymeleafProperties properties, ObjectProvider<ITemplateResolver> templateResolvers, ObjectProvider<IDialect> dialects) {
SpringTemplateEngine engine = new SpringTemplateEngine();
engine.setEnableSpringELCompiler(properties.isEnableSpringElCompiler());
engine.setRenderHiddenMarkersBeforeCheckboxes(properties.isRenderHiddenMarkersBeforeCheckboxes());
templateResolvers.orderedStream().forEach(engine::addTemplateResolver);
dialects.orderedStream().forEach(engine::addDialect);
return engine;
}
- 5.好了代碼寫完,執行@Test方法後,查看 139 郵箱結果
結束
這樣一份可以修改模板的郵件就完成了,當然是用的 Thymeleaf ,另外一種在 Spring Boot 常用的模板是 Freemarker ,它可沒有像 Thymeleaf 那樣自動化配置較多,更多的是要手工完成,下一篇,也是最後一篇介紹