1.爲何要寫這一篇呢?css
在作一個郵件發送功能的時候,須要發送html郵件,javaMail 發送html 的時候須要有已經生成的html正文,因此須要提早將要發送的內容生成,因此就須要模板引擎來動態填充數據。html
public void sendHtmlEmail(String to, String object, String content) { MimeMessage message = mailSender.createMimeMessage();//建立一個MINE消息 try { //true表示須要建立一個multipart message MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(from); helper.setTo(to); helper.setSubject(object); helper.setText(content, true); mailSender.send(message); log.info("html郵件發送成功"); } catch (MessagingException e) { log.error("發送html郵件時發生異常!", e); } }
2.引入依賴java
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
ognl 的jar包可能並不須要,在生成過程當中報classNotFound ,應該是和個人項目結構有關係,這個包根據實際狀況來使用spring
<dependency> <groupId>ognl</groupId> <artifactId>ognl</artifactId> <version>3.2</version> </dependency>
3.代碼springboot
(1)首先準備一個html模板,須要替換的內容使用themleaf的th表達式來佔位。個人工程是springboot 項目,文件位置放在resources/templates,也就是classpath:templates/app
<!DOCTYPE html> <html lang="zh" xmlns:th="http://www.thymeleaf.org"> <html> <head> <title>Title</title> <style type="text/css"> td{ width: 80px; height: 25px; align-content: center; } </style> </head> <body> <h4>親愛的<span th:text="${name}"></span>,您<span th:text="${month}"></span>月份的工資以下:</h4> <table border="1" style="border-collapse: collapse" > <tr> <td th:each="item:${th}" th:text="${item}"></td> </tr> <tr> <td th:each="ib:${tb}" th:text="${ib}"></td> </tr> </table> </body> </html>
2.配置模板引擎spring-boot
@Configuration public class MailConfig { @Bean("myTemplateEngine") public TemplateEngine templateEngine(){ ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver(); resolver.setPrefix("templates/"); resolver.setSuffix(".html"); TemplateEngine engine = new TemplateEngine(); engine.setTemplateResolver(resolver); return engine; } }
爲手動生成html文件單獨定義一個模板引擎,其餘的使用默認配置。其中制定了模板文件的位置及後綴名,這個配置和application.properties 中配置是同樣的測試
3.生成htmlthis
@PostMapping("/sendEmail") @ResponseBody public AjaxResult sendEmail(Integer id) throws IOException { List<ComponentVo> componentVos = salaryCompnentService.selectComponentSum(id); List<String> th = Lists.newArrayList(); List<String> tb = Lists.newArrayList(); for (int i = 0; i < componentVos.size(); i++) { ComponentVo vo = componentVos.get(i); th.add(i, vo.getName()); tb.add(i, vo.getAmount()); } SalaryCalculateVo salaryCalculate = salaryService.selectSalaryById(id); Context context = new Context(); context.setVariable("name", salaryCalculate.getEmpName()); context.setVariable("month", salaryCalculate.getWorkMonth()); context.setVariable("th", th); context.setVariable("tb", tb); String res = engine.process("mailTeamlate", context); salaryService.sendEmail(id,res, salaryCalculate.getWorkMonth() + "工資條", salaryCalculate.getEmail()); return toAjax(1); }
因爲是測試功能,沒有在代碼結構上下功夫,隨便寫一下。主要的代碼是王模板上下文Context中填充參數,engine.process()有不少重載的方法,主要有兩類,一類是直接輸出內容,一類是將文件輸出到指定文件裏。String template 這個參數指的是模板的名稱。好比個人叫mailTeamlate,解析的時候模板引擎會找classpath:templates/mailTeamlate.html 這個文件。spa
public final String process(String template, IContext context) { return this.process(new TemplateSpec(template, (Set)null, (TemplateMode)null, (String)null, (Map)null), context); }
public final void process(String template, IContext context, Writer writer) { this.process(new TemplateSpec(template, (Set)null, (TemplateMode)null, (String)null, (Map)null), context, writer); }
4.效果
具體的文件內容就不說了,直接看我放到郵件的裏面正文裏的html樣式