SpringBoot使用FreeMarker模板發(fā)送郵件
本文實例為大家分享了SpringBoot +Mail+FreeMarker發(fā)送郵件,供大家參考,具體內(nèi)容如下
通過spirngboot 自帶的mail服務(wù)及FreeMarker模板引擎,發(fā)送郵
添加依賴
<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服務(wù)
@Service public class MailServiceImpl implements MailService { //郵件的發(fā)送者 @Value("${spring.mail.username}") private String from; //注入MailSender @Autowired private JavaMailSender mailSender; //發(fā)送郵件的模板引擎 @Autowired private FreeMarkerConfigurer configurer; /** * @param params 發(fā)送郵件的主題對象 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"));//發(fā)送給誰 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(); } } }
定義發(fā)送郵件對象
發(fā)送內(nèi)容為object,我這里演示一個對象,通過模板渲染方式接收內(nèi)容
@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>
測試郵件發(fā)送
新建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("缺少參數(shù),請確認"); mailService.sendMessageMail(message, "測試消息通知", "message.ftl"); } }
啟動服務(wù)訪問 http://localhost:8080/sendMessage
查看郵箱
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
springboot項目配置logback-spring.xml實現(xiàn)按日期歸檔日志的方法
本文主要介紹了springboot項目配置logback-spring.xml實現(xiàn)按日期歸檔日志的方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-08-08解決MyEclipse6.5無法啟動,一直停留剛開始啟動界面的詳解
本篇文章是對解決MyEclipse6.5無法啟動,一直停留剛開始啟動界面的方法進行了詳細的分析介紹,需要的朋友參考下2013-05-05springboot配置多數(shù)據(jù)源后mybatis攔截器失效的解決
這篇文章主要介紹了springboot配置多數(shù)據(jù)源后mybatis攔截器失效的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09Spring?Boot?集成?Quartz并使用Cron?表達式實現(xiàn)定時任務(wù)
本篇文章介紹了如何在?Spring?Boot?中集成?Quartz?進行定時任務(wù)調(diào)度,并通過?Cron?表達式?控制任務(wù)執(zhí)行時間,Quartz?提供了更強大的任務(wù)調(diào)度能力,比?@Scheduled?注解更靈活,適用于復(fù)雜的定時任務(wù)需求2025-04-04對ArrayList和LinkedList底層實現(xiàn)原理詳解
今天小編就為大家分享一篇對ArrayList和LinkedList底層實現(xiàn)原理詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10java socket接收保證能讀完數(shù)據(jù)的實例
這篇文章主要介紹了java socket接收保證能讀完數(shù)據(jù)的實例,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10