SpringBoot整合FreeMarker发送邮件
1 添加依赖
pom.xml中加入如下依赖:
org.springframework.boot
spring-boot-starter-mail
org.springframework.boot
spring-boot-starter-freemarker
2 配置Freemarker
application.yml配置文件中加入如下配置:
Spring:
#邮箱地址配置
mail:
host: 邮箱服务器地址
port: 邮箱服务器端口
username: 邮箱服务器用户名
password: 邮箱服务器密码
#编码格式
default-encoding: utf-8
#freemarker默认存放路径
freemarker:
template-loader-path: classpath:/templates
3.测试类
(1) 创建测试类
测试类代码 :
@SpringBootTest
public class FreeMarkerTest {
// 自动注入FreeMarker配置类,用户获取模板
@Autowired
private Configuration configuration;
// 注入Spring发送邮件的工具类
@Autowired
private JavaMailSender sender;
@Test
@DisplayName("测试邮件模板")
public void testMailFreeMark() throws Exception{
//初始化数据
List
(2) 在resource下创建templates目录,并创建test.ftl模板。
模板代码 :
Dear Mr./Ms. ${username},
以下为最近7天内即将到期关闭的任务,请及时关注,谢谢
<#list rows>
| 代码 | 任务名称 | 到期日期 | 剩余天数 |
<#items as row>
| ${row.id} |
${row.name} |
${row.expirydate} |
${row.surplus} |
#items>
#list>
IT
If you have any IT related questions, please send your request to
Baidu.
效果