SpringBoot使用FreeMarker模板发送邮件
人气:0通过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 自定义权限标签(tld),在freemarker引用操作
- SpringBoot2 整合FreeMarker实现页面静态化示例详解
- Springboot整合freemarker 404问题解决方案
- SpringBoot2.2.X用Freemarker出现404的解决
- 构建SpringBoot+MyBatis+Freemarker的项目详解
- SpringBoot整合freemarker的讲解
- spring boot 集成 shiro 自定义密码验证 自定义freemarker标签根据权限渲染不同页面(推荐
- 详解MyEclipse中搭建spring-boot+mybatis+freemarker框架
- Spring FreeMarker整合Struts2过程详解
加载全部内容