主要在于构建HTML模板,新建类似模板:
<table border='0' th:each='data:${datas}' cellspacing='1' cellpadding='0'
style='background:#DCDFE6; line-height: 30px;width: 900px; left:auto;'>
<tr style="color: #000000;text-align: left;font-weight: bold;">
<td colspan='4'>任务名称:<span th:text='${data.taskName}'/></td>
</tr>
<tr style="text-align: left;">
<th style='background:#FFF;width: 80px; '>ID</th>
<th style='background:#FFF;'>名称</th>
<th style='background:#FFF;width: 60px;'>时间</th>
<th style='background:#FFF;width: 60px;'>数量</th>
</tr>
<tr th:each='item:${data.list}' style="text-align: left;">
<td style="background: #FFF;width: 80px;" th:text='${item.id}'></td>
<td style="background: #FFF;" th:text='${item.name}'></td>
<td style="background: #FFF;width: 60px;" th:text='${item.time}'></td>
<td style="background: #FFF;width: 60px;" th:text='${item.count}'></td>
</tr>
<br />
</table>
使用 thymeleaf 加载HTML,并赋值构造完整HTML页面。
引入插件:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>2.4.3</version>
</dependency>
构建HTML
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
Context context1 = new Context();
context1.setVariable("datas",list);
String content = templateEngine.process(mailInfo.getMailContent(),context1);
然后发送邮件
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/289660.html