SpringBoot使用FreeMarker模板发送邮件
作者:if_where 时间:2021-07-27 07:32:26
本文实例为大家分享了SpringBoot +Mail+FreeMarker发送邮件,供大家参考,具体内容如下
通过spirngboot 自带的mail服务及FreeMarker模板引擎,发送邮
添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--消除冗余代码使用-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
在application.yml文件中配置Mail信息
spring:
mail:
port: 25
username: ${username}
password: ${password}
protocol: smtp
default-encoding: utf-8
host: ${host}
编写MailService服务
@Service
public class MailServiceImpl implements MailService {
//邮件的发送者
@Value("${spring.mail.username}")
private String from;
//注入MailSender
@Autowired
private JavaMailSender mailSender;
//发送邮件的模板引擎
@Autowired
private FreeMarkerConfigurer configurer;
/**
* @param params 发送邮件的主题对象 object
* @param title 邮件标题
* @param templateName 模板名称
*/
@Override
public void sendMessageMail(Object params, String title, String templateName) {
try {
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
helper.setFrom(from);
helper.setTo(InternetAddress.parse("xxxxx@163.com"));//发送给谁
helper.setSubject("【" + title + "-" + LocalDate.now() + " " + LocalTime.now().withNano(0) + "】");//邮件标题
Map<String, Object> model = new HashMap<>();
model.put("params", params);
try {
Template template = configurer.getConfiguration().getTemplate(templateName);
try {
String text = FreeMarkerTemplateUtils.processTemplateIntoString(template, model);
helper.setText(text, true);
mailSender.send(mimeMessage);
} catch (TemplateException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
定义发送邮件对象
发送内容为object,我这里演示一个对象,通过模板渲染方式接收内容
@Data
public class Message {
private String messageCode;
private String messageStatus;
private String cause;
}
在项目templates目录新建个message.ftl文件
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>消息通知</title>
</head>
<style type="text/css">
table {
font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
width: 100%;
border-collapse: collapse;
}
td, th {
font-size: 1em;
border: 1px solid #5B4A42;
padding: 3px 7px 2px 7px;
}
th {
font-size: 1.1em;
text-align: center;
padding-top: 5px;
padding-bottom: 4px;
background-color: #24A9E1;
color: #ffffff;
}
</style>
<body>
<div>
<h2>邮件消息通知</h2>
<table id="customers">
<tr>
<th>MessageCode</th>
<th>MessageStatus</th>
<th>Cause</th>
</tr>
<tr>
<td>${(params.messageCode)!""}</td>
<td>${(params.messageStatus)!""}</td>
<td>${(params.cause)!""}</td>
</tr>
</table>
</div>
</body>
</html>
测试邮件发送
新建controller类
@RestController
public class MailController {
@Autowired
private MailService mailService;
@RequestMapping(value = "/sendMessage", method = RequestMethod.GET)
public void sendMailMessage() {
Message message = new Message();
message.setMessageCode("MissingParameter");
message.setMessageStatus("Failed");
message.setCause("缺少参数,请确认");
mailService.sendMessageMail(message, "测试消息通知", "message.ftl");
}
}
启动服务访问 http://localhost:8080/sendMessage
查看邮箱
源码地址
来源:https://blog.csdn.net/system_linux/article/details/78792028
标签:SpringBoot,FreeMarker,发送邮件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
jenkins+Maven从SVN上构建项目的方法
2022-07-09 04:42:37
![](https://img.aspxhome.com/file/2023/2/61582_0s.png)
SpringBoot整合Mybatis实现多数据源配置与跨数据源事务实例
2023-06-29 23:47:34
java用户名密码验证示例代码分享
2021-10-14 08:16:14
android Retrofit2+okHttp3使用总结
2023-07-31 21:02:10
java实现mongodb的数据库连接池
2023-11-23 14:23:09
java二维数组指定不同长度实例方法
2021-07-13 06:02:09
java图片滑动验证(登录验证)原理与实现方法详解
2023-07-10 13:29:53
![](https://img.aspxhome.com/file/2023/8/72538_0s.jpg)
springboot vue 跨域问题的解决
2023-01-26 00:53:57
java用类加载器的5种方式读取.properties文件
2022-05-06 14:55:19
![](https://img.aspxhome.com/file/2023/9/75419_0s.png)
在IntelliJ IDEA中使用gulp的方法步骤(图文)
2022-10-12 06:29:08
![](https://img.aspxhome.com/file/2023/0/60770_0s.png)
Java如何有效避免SQL注入漏洞的方法总结
2021-11-22 07:01:54
Spring bean的实例化和IOC依赖注入详解
2023-11-23 23:57:15
java如何使用自己的maven本地仓库详解
2022-08-01 12:13:44
使用Spring Boot进行单元测试详情
2023-11-10 08:01:53
springboot +rabbitmq+redis实现秒杀示例
2022-04-21 02:29:28
![](https://img.aspxhome.com/file/2023/0/79940_0s.jpg)
基于Scala和Java方法的相互调用
2021-07-05 11:53:04
Spring JDK动态 代理实现过程详解
2023-11-16 19:42:15
![](https://img.aspxhome.com/file/2023/2/59392_0s.png)
C#如何将DataTable导出到Excel解决方案
2022-03-30 18:44:28
Flutter倒计时/计时器的实现代码
2023-07-01 03:50:50
![](https://img.aspxhome.com/file/2023/5/103895_0s.jpg)
Mybatis配置文件之动态SQL配置备忘录
2023-11-21 00:13:02