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