javascript
java 邮件模板_Spring Boot 优雅地发送邮件
最近在項目開發中有向使用者發送報警通知的功能,其中報警媒介就包括郵件,這篇文章就簡單介紹了 Spring Boot 如何快速集成實現郵件發送。
通常在實際項目中,也有其他很多地方會用到郵件發送,比如通過郵件注冊賬戶/找回密碼,通過郵件發送訂閱信息等等。
開發前準備
下面以 QQ 郵箱為例,其他的郵箱的配置也大同小異。
登錄 QQ 郵箱,點擊設置->賬戶,開啟IMAP/SMTP服務,并生成授權碼。
準備工作做好后,就開始進行項目實戰吧!
Spring Boot 集成郵件發送
Spring Boot 集成郵件發送主要分為以下三步:
加入依賴
首先創建一個 Spring Boot 項目,然后在 pom.xml 加入如下依賴(其中 thymeleaf 是為了發送模版郵件):
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId> </dependency> <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>創建郵件配置
在配置文件 application.properties 中配置郵件的相關參數,具體內容如下:
# 使用 smtp 協議 spring.mail.protocol = smtp spring.mail.host = smtp.qq.com spring.mail.port = 587 spring.mail.username = wupx@qq.com # 授權碼 spring.mail.password = yourauthorizationcode spring.mail.test-connection = false spring.mail.properties.mail.smtp.auth = false spring.mail.properties.mail.debug = false spring.mail.properties.mail.mime.splitlongparameters = false spring.mail.default-encoding = UTF-8其中指定了郵件的協議、端口以及郵件賬戶和授權碼等。
服務類
在這里主要介紹發送簡單郵件、模板等操作,在 service 包下創建 Mailervice 類。
發送簡單郵件
首先注入 JavaMailSender,然后構造 SimpleMailMessage ,調用 javaMailSender 的 send 方法就可以完成簡單的郵件發送,具體代碼如下所示:
public void sendSimpleMail(String from, String to, String subject, String text) {SimpleMailMessage simpleMailMessage = new SimpleMailMessage();// 發件人simpleMailMessage.setFrom(from);// 收件人simpleMailMessage.setTo(to);// 郵件主題simpleMailMessage.setSubject(subject);// 郵件內容simpleMailMessage.setText(text);javaMailSender.send(simpleMailMessage); }編寫對應的 Controller 層,代碼如下:
@GetMapping("/sendSimpleMail") public void sendSimpleMail() {mailService.sendSimpleMail("wupx@qq.com","huxy@qq.com","歡迎關注微信公眾號「武培軒」","感謝你這么可愛,這么優秀,還來關注我,關注了就要一起成長哦~~回復【資料】領取優質資源!"); }調用發送簡單郵件接口后,就可以在 QQ 郵箱中收到郵件:
發送復雜郵件
雖然簡單的純文本郵件已經基本夠用了,但是有的時候,也需要很多樣式來豐富郵件,接下來就來演示下發送復雜郵件(文本+圖片+附件)。
首先使用 javaMailSender 創建一個 MimeMessage 實例,用 MimeMessageHelper 設置開啟內嵌和附件功能,然后通過 addInline 方法嵌入圖片(其中 logo 需要與 text 中的 cid 對應起來),addAttachment 方法添加附件,具體代碼如下:
public ResponseEntity<String> sendMimeMail(String from, String to, String subject, String text) {MimeMessage mimeMessage = javaMailSender.createMimeMessage();try {MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);helper.setFrom(from);helper.setTo(to);helper.setSubject(subject);// 設置郵件內容,第二個參數設置是否支持 text/html 類型helper.setText(text, true);helper.addInline("logo", new ClassPathResource("img/logo.jpg"));helper.addAttachment("logo.pdf", new ClassPathResource("doc/logo.pdf"));javaMailSender.send(mimeMessage);return ResponseEntity.status(HttpStatus.CREATED).body("發送成功");} catch (MessagingException e) {e.printStackTrace();return ResponseEntity.status(HttpStatus.NOT_FOUND).body("e.getMessage()");} }編寫對應的 Controller 層,代碼如下:
@GetMapping("/sendMimeMail") public ResponseEntity<String> sendMimeMail() {return mailService.sendMimeMail("wupx@qq.com","huxy@qq.com","歡迎關注微信公眾號「武培軒」","<h3>感謝你這么可愛,這么優秀,還來關注我,關注了就要一起成長哦~~</h3><br>" +"回復【資料】領取優質資源!<br>" +"<img src='cid:logo'>"); }調用發送復雜郵件接口后,就可以在 QQ 郵箱中收到郵件:
發送模板郵件
Java 的模板引擎有許多,在這里使用的是 Thymeleaf,注入 TemplateEngine,使用它來解析模版,然后將返回的字符串作為內容發送,具體代碼如下:
public ResponseEntity<String> sendTemplateMail(String from, String to, String subject, Context context) {MimeMessage message = javaMailSender.createMimeMessage();try {MimeMessageHelper helper = new MimeMessageHelper(message, true);helper.setFrom(from);helper.setTo(to);helper.setSubject(subject);// 解析郵件模板String text = templateEngine.process("mailTemplate", context);helper.setText(template, true);javaMailSender.send(message);return ResponseEntity.status(HttpStatus.CREATED).body("發送成功");} catch (Exception e) {e.printStackTrace();return ResponseEntity.status(HttpStatus.NOT_FOUND).body("e.getMessage()");} }編寫對應的 Controller 層,代碼如下:
@GetMapping("/sendTemplateMail") public ResponseEntity<String> sendTemplateMail() {Context context = new Context();context.setVariable("username", "武培軒");return mailService.sendTemplateMail("wupx@qq.com","huxy@qq.com","歡迎關注微信公眾號「武培軒」",context); }調用發送模板郵件接口后,就可以在 QQ 郵箱中收到郵件:
總結
本文的完整代碼在 https://github.com/wupeixuan/SpringBoot-Learn 的 mail 目錄下。
Spring Boot 集成郵件發送還是比較簡單的,大家可以下載項目源碼,自己在本地運行調試這個項目,更好地理解如何在 Spring Boot 中開發郵件服務。
最好的關系就是互相成就,大家的點贊、在看、分享、留言就是我創作的最大動力。
參考https://github.com/wupeixuan/SpringBoot-Learn 超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生總結
以上是生活随笔為你收集整理的java 邮件模板_Spring Boot 优雅地发送邮件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python可视化拖拽平台_【技术解码】
- 下一篇: spring boot plugin_s