欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SpringBoot使用FreeMarker模板发送邮件

程序员文章站 2024-02-23 22:00:40
本文实例为大家分享了springboot +mail+freemarker发送邮件,供大家参考,具体内容如下 通过spirngboot 自带的mail服务及freemar...

本文实例为大家分享了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

查看邮箱

SpringBoot使用FreeMarker模板发送邮件

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。