当前位置: 移动技术网 > IT编程>开发语言>Java > SpringBoot使用FreeMarker模板发送邮件

SpringBoot使用FreeMarker模板发送邮件

2019年07月19日  | 移动技术网IT编程  | 我要评论

本文实例为大家分享了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://www.lhsxpumps.com/_localhost:8080/sendmessage

查看邮箱

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网