不懂SpringBoot如何使用FreeMarker模塊引擎發(fā)送郵件??其實想解決這個問題也不難,下面讓小編帶著大家一起學(xué)習(xí)怎么去解決,希望大家閱讀完這篇文章后大所收獲。
網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)!專注于網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、微信小程序開發(fā)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了淳安免費建站歡迎大家使用!
通過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 郵件標(biāo)題 * @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) + "】");//郵件標(biāo)題 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> <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>
測試郵件發(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ù),請確認(rèn)"); mailService.sendMessageMail(message, "測試消息通知", "message.ftl"); } }
啟動服務(wù)訪問 http://localhost:8080/sendMessage
查看郵箱
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享SpringBoot如何使用FreeMarker模塊引擎發(fā)送郵件?內(nèi)容對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,遇到問題就找創(chuàng)新互聯(lián),詳細(xì)的解決方法等著你來學(xué)習(xí)!
分享文章:SpringBoot如何使用FreeMarker模塊引擎發(fā)送郵件?
文章分享:http://jinyejixie.com/article0/pgijio.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、用戶體驗、軟件開發(fā)、服務(wù)器托管、搜索引擎優(yōu)化、動態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)