您好,登錄后才能下訂單哦!
不懂SpringBoot如何使用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> <h3>郵件消息通知</h3> <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模塊引擎發送郵件?內容對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,遇到問題就找億速云,詳細的解決方法等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。