问题:
如何用java发送包含表格形式的邮件?
方法:
发用freemaker工具来替换html的表格变量,从而动态生成了html。然后再发送这个html格式(不能用纯文本)文本即可。
优化流程:
1、准备模板(这里是以Excel转html为模板)
2、处理模板及对模板填充内容的工具类
3、修改发送邮件的代码
所需依赖
pom.xml依赖
<!-- freemarker -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.4</version>
</dependency>
具体实现
1、准备模板
先用excel画一个模板,如下图

然后在网上找一个excel转换html的在线转换工具
我用的是:零代码 - Table在线布局工具(Excel转HTML)
2. 转换成功之后下载,之后会得到这样一个html文件。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>www.lingdaima.com(零代码excel转HTML)</title>
<style>
table{
border-top:1px solid #333;border-left:1px solid #333;border-spacing:0;background-color:#fff;width:100%}
table td{
border-bottom:1px solid #333;border-right:1px solid #333;font-size:13px;padding:5px}
.font5{
color:windowtext;}
.xl65{
text-align:left ;}
</style>
</head>
<body>
<table style="width:500pt"> <!--StartFragment-->
<colgroup>
<col width="111" style="mso-width-source:userset;mso-width-alt:3552;width:83pt">
<col width="131" style="mso-width-source:userset;mso-width-alt:4192;width:98pt">
<col width="290" style="mso-width-source:userset;mso-width-alt:9280;width:218pt">
<col width="135" style="mso-width-source:userset;mso-width-alt:4320;width:101pt">
</colgroup>
<tbody>
<tr height="19">
<td class="xl65">号码</td>
<td class="xl65">所属运营商</td>
<td class="xl65">所属号码组</td>
<td class="xl65">报警时间</td>
</tr>
<tr height="19">
<td class="xl65">15528474394</td>
<td class="xl65">万事融通</td>
<td class="xl65">智慧外呼_万事融通_日产专用_品牌认证</td>
<td class="xl65">2024/10/24 9:35</td>
</tr> <!--EndFragment-->
</tbody>
</table>
</body>
</html>
3.把这个文件修改为 alert-email-template.ftl, 放到 java 项目的 resouce/ftl/目录下
该文件的内容为
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
table{
border-top:1px solid #333;border-left:1px solid #333

最低0.47元/天 解锁文章
1328

被折叠的 条评论
为什么被折叠?



