SpringBoot實現定時發(fā)送郵件的三種方法案例詳解
一、發(fā)送郵件的三種方法
1、發(fā)送純文本郵件
2、發(fā)送復雜郵件
3、發(fā)送模板郵件
二、定時任務介紹
Spring框架的定時任務調度功能支持配置和注解兩種方式Spring Boot在Spring框架的基礎上實現了繼承,并對其中基于注解方式的定時任務實現了非常好的支持。下面,針對 Spring Boot 項目中基于注解方式的定時任務調度的相關注解和使用進行介紹。
1.@EnableScheduling
@EnableScheduling 注解是 Spring 框架提供的,用于開啟基于注解方式的定時任務支持,該注解主要用在項目啟動類上。
2.@Scheduled
@Scheduled 注解同樣是 Spring 框架提供的,配置定時任務的執(zhí)行規(guī)則,該注解主要用在定時業(yè)務方法上。@Scheduled 注解提供有多個屬性,精細化配置定時任務執(zhí)行規(guī)則
屬性 | 說明 |
cron | 類似于 cron 的表達式,可以定制定時任務觸發(fā)的秒、分鐘、小時、月中的日、月、周中的日 |
zone | 表示在上一次任務執(zhí)行結束后在指定時間后繼續(xù)執(zhí)行下一次任務(屬性值為long類型) |
fixedDelay | 指定cron 表達式將被解析的時區(qū)。默認情況下,該屬性是空字符串(即使用服務器的本地時區(qū) |
fixedDelayString | 表示在上一次任務執(zhí)行結束后在指定時間后繼續(xù)執(zhí)行下一次任務(屬性值為long類型的字符串形式) |
fixedRate | 表示每隔指定時間執(zhí)行一次任務 (屬性值為 long 類型) |
fixedRateString | 表示每隔指定時間執(zhí)行一次任務(屬性值為 long 類型的字符串形式) |
initialDelay | 表示在fixedRate 或fixedDelay 任務第一次執(zhí)行之前要延遲的毫秒數(屬性值為long類型) |
initialDelayString | 表示在fixedRate或fixedDelay 任務第一次執(zhí)行之前要延遲的毫秒數(屬性值為long類型的字符串形式) |
三、前期準備工作
1、登錄QQ郵箱獲取授權碼
第一步:進入QQ郵箱
第二步:找到POP3/SMTP,并開啟
第三步:復制授權碼
開啟過程需要手機號碼驗證,按照步驟操作即可。開啟成功之后,即可獲取一個授權碼,將該號碼保存好,一會使用
2、pom.xml中的依賴
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--添加下面的依賴后,Spring Boot自動配置的郵件服務會生效,在郵件發(fā)送任務時, 可以直接使用Spring框架提供的JavaMailSender接口或者它的實現類JavaMailSenderImpl郵件 發(fā)送--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> </dependencies>
3、在全局配置文件application.properties添加郵件服務配置
# 發(fā)件人郵件服務器相關配置 spring.mail.host=smtp.qq.com spring.mail.port=587 # 配置個人QQ賬戶和密碼(這里需要大家修改為自己的QQ賬號和密碼,密碼是加密后的授權碼,授權碼的獲得后繼講解) spring.mail.username=QQ@qq.com spring.mail.password=填入剛剛復制的授權碼 spring.mail.default-encoding=UTF-8 # 郵件服務超時時間配置 spring.mail.properties.mail.smtp.connectiontimeout=5000 spring.mail.properties.mail.smtp.timeout=3000 spring.mail.properties.mail.smtp.writetimeout=5000
四、操作
一、創(chuàng)建郵件發(fā)送任務管理的業(yè)務處理類SendEmailService
注意:在方法上的注解@Async是需要搭配定時任務一起使用的,如果使用普通的test類時可以不用這個注解的
package com.lyn.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.io.FileSystemResource; import org.springframework.mail.MailException; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSenderImpl; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import java.io.File; /** * @author:Lyn.R * @date:2023-02-21 14:54:36 * @Description: * @note: **/ @Service public class SendEmailService { @Autowired private JavaMailSenderImpl mailSender;//使用Spring框架提供的實現類JavaMailSenderImpl來實現郵件發(fā)送。 @Value("${spring.mail.username}")//借助@Value注解讀取全局變量中的spring.mail.username的值來作發(fā)件人 private String from; /** * 第一種方法:發(fā)送純文本郵件 * @param to 收件人地址 * @param subject 郵件標題 * @param text 郵件內容 */ @Async public void sendSimpleEmail(String to, String subject, String text) { // 定制純文本郵件信息SimpleMailMessage SimpleMailMessage message = new SimpleMailMessage(); message.setFrom(from);//設置發(fā)件人 message.setTo(to);//設置收件人 message.setSubject(subject);//設置郵件標題 message.setText(text);//設置 正文件內容 try { // 發(fā)送郵件 mailSender.send(message); System.out.println("純文本郵件發(fā)送成功"); } catch (MailException e) { System.out.println("純文本郵件發(fā)送失敗 " + e.getMessage()); e.printStackTrace(); } } /** * 第二種方法:發(fā)送復雜郵件(包括靜態(tài)資源和附件) * @param to 收件人地址 * @param subject 郵件標題 * @param text 郵件內容 * @param filePath 附件地址 * @param rscId 靜態(tài)資源唯一標識 * @param rscPath 靜態(tài)資源地址 */ //sendComplexEmail()方法需要接收的參數除了基本的發(fā)送信息外,還包括靜態(tài)資源唯一標識、靜態(tài)資源路徑和附件路徑 @Async public void sendComplexEmail(String to,String subject,String text,String filePath,String rscId,String rscPath){ // 定制復雜郵件信息MimeMessage MimeMessage message = mailSender.createMimeMessage(); try { // 使用MimeMessageHelper幫助類對郵件信息封裝處理 ,并設置multipart多部件使用為true MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(from); helper.setTo(to); helper.setSubject(subject); helper.setText(text, true); // 設置郵件靜態(tài)資源 FileSystemResource res = new FileSystemResource(new File(rscPath)); helper.addInline(rscId, res);//設置郵件靜態(tài)資源的方法 // 設置郵件附件 FileSystemResource file = new FileSystemResource(new File(filePath)); String fileName = filePath.substring(filePath.lastIndexOf(File.separator)); helper.addAttachment(fileName, file);//設置郵件附件的方法 // 發(fā)送郵件 mailSender.send(message); System.out.println("復雜郵件發(fā)送成功"); } catch (MessagingException e) { System.out.println("復雜郵件發(fā)送失敗 "+e.getMessage()); e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } /** * 第三鐘方法:發(fā)送模板郵件 * @param to 收件人地址 * @param subject 郵件標題 * @param content 郵件內容 */ @Async public void sendTemplateEmail(String to, String subject, String content) { MimeMessage message = mailSender.createMimeMessage(); try { // 使用MimeMessageHelper幫助類對郵件信息進行封裝處理,并設置multipart多部件使用為true MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(from); helper.setTo(to); helper.setSubject(subject); helper.setText(content, true); // 發(fā)送郵件 mailSender.send(message); System.out.println("模板郵件發(fā)送成功"); } catch (MessagingException e) { System.out.println("模板郵件發(fā)送失敗 "+e.getMessage()); e.printStackTrace(); } } }
二、在test類中發(fā)送郵件
package com.lyn; import com.lyn.service.SendEmailService; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.thymeleaf.TemplateEngine; import org.thymeleaf.context.Context; @SpringBootTest class SpringbootHomeworkEmail0221ApplicationTests { @Autowired private SendEmailService sendEmailService; @Test public void sendSimpleMailTest() { String to="12345678@qq.com";//這里修改為你能接收到的郵箱 String subject="【純文本郵件】標題"; String text="嘟嘟嘟....."; // 發(fā)送簡單郵件 sendEmailService.sendSimpleEmail(to,subject,text); } @Test public void sendComplexEmailTest() { //根據前面定義的復雜郵件發(fā)送業(yè)務定制各種參數 String to="12345678@qq.com";//修改為你自己的郵件方便接收查看 String subject="【復雜郵件】標題"; // 定義郵件內容 StringBuilder text = new StringBuilder(); //對郵件內容使用了HTML標簽編輯郵件內容 text.append("<html><head></head>"); text.append("<body><h1>二月二龍?zhí)ь^!</h1>"); // cid為嵌入靜態(tài)資源文件關鍵字的固定寫法,如果改變將無法識別;rscId則屬于自定義的靜態(tài)資源唯一標識,一個郵件內容中可能會包括多個靜態(tài)資源,該屬性是為了區(qū)別唯一性的。 String rscId = "img001"; text.append("<img src='cid:" +rscId+"'/></body>"); text.append("</html>"); // 指定靜態(tài)資源文件和附件路徑 String rscPath="D:\\1.jpg";//注意這里修改為你的硬盤中有的資源 String filePath="D:\\hahaha.txt";//注意這里修改為你的硬盤中有的資源 // 發(fā)送復雜郵件 sendEmailService.sendComplexEmail(to,subject,text.toString(),filePath,rscId,rscPath); } @Autowired private TemplateEngine templateEngine; @Test public void sendTemplateEmailTest() { String to="12345678@qq.com"; String subject="【模板郵件】標題"; // 使用模板郵件定制郵件正文內容 Context context = new Context();//Context注意正確導入“import org.thymeleaf.context.Context;” context.setVariable("username", "石頭"); context.setVariable("code", "456123"); // 使用TemplateEngine設置要處理的模板頁面 String emailContent = templateEngine.process("emailTemplate_vercode", context); // 發(fā)送模板郵件 sendEmailService.sendTemplateEmail(to,subject,emailContent); } }
模板文件的html(emailTemplate_vercode.html)
<!DOCTYPE html> <html lang="en"> <html lang="zh" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"/> <title>用戶驗證碼</title> </head> <body> <div><span th:text="${username}">XXX</span> 先生/女士,您好:</div> <P style="text-indent: 2em">您的新用戶驗證碼為<span th:text="$[code]" style="color: cornflowerblue">123456</span>,請妥善保管。</P> </body> </html>
三、發(fā)送定時郵件
下面類中的 @Scheduled(cron = "*/5 * * * * ?")表達式大家可以去下面的網址生成Cron - 在線Cron表達式生成器 (ciding.cc)
package com.lyn.controller; import com.lyn.service.SendEmailService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Controller; import org.springframework.stereotype.Service; import org.thymeleaf.TemplateEngine; import org.thymeleaf.context.Context; /** * @author:Lyn.R * @date:2023-02-21 19:55:01 * @Description: * @note: **/ @Controller public class MyScheduled { @Autowired private SendEmailService sendEmailService; @Autowired //模板引擎(Template Engine), 是用來解析對應類型模板文件然后動態(tài)生成由數據和靜態(tài)頁面組成的視圖文件的一個工具 private TemplateEngine templateEngine; @Scheduled(cron = "*/5 * * * * ?") public void sendSimpleMailTest() { String to="12345678@qq.com";//這里修改為你能接收到的郵箱 String subject="【純文本郵件】標題"; String text="嘟嘟嘟....."; // 發(fā)送簡單郵件 sendEmailService.sendSimpleEmail(to,subject,text); } @Scheduled(cron = "1 * * * * ? ") public void sendComplexEmailTest() { //根據前面定義的復雜郵件發(fā)送業(yè)務定制各種參數 String to="12345678@qq.com";//修改為你自己的郵件方便接收查看 String subject="【復雜郵件】標題"; // 定義郵件內容 StringBuilder text = new StringBuilder(); //對郵件內容使用了HTML標簽編輯郵件內容 text.append("<html><head></head>"); text.append("<body><h1>二月二龍?zhí)ь^!</h1>"); // cid為嵌入靜態(tài)資源文件關鍵字的固定寫法,如果改變將無法識別;rscId則屬于自定義的靜態(tài)資源唯一標識,一個郵件內容中可能會包括多個靜態(tài)資源,該屬性是為了區(qū)別唯一性的。 String rscId = "img001"; text.append("<img src='cid:" +rscId+"'/></body>"); text.append("</html>"); // 指定靜態(tài)資源文件和附件路徑 String rscPath="D:\\1.jpg";//注意這里修改為你的硬盤中有的資源 String filePath="D:\\hahaha.txt";//注意這里修改為你的硬盤中有的資源 // 發(fā)送復雜郵件 sendEmailService.sendComplexEmail(to,subject,text.toString(),filePath,rscId,rscPath); } @Scheduled(cron = "0 * * * * ? ") public void sendTemplateEmailTest() { String to="12345678@qq.com"; String subject="【模板郵件】標題"; // 使用模板郵件定制郵件正文內容 Context context = new Context();//Context注意正確導入“import org.thymeleaf.context.Context;” context.setVariable("username", "石頭"); context.setVariable("code", "456123"); // 使用TemplateEngine設置要處理的模板頁面 String emailContent = templateEngine.process("emailTemplate_vercode", context); // 發(fā)送模板郵件 sendEmailService.sendTemplateEmail(to,subject,emailContent); } }
四、在項目啟動類上添加基于注解的定時任務支持:@EnableScheduling
package com.lyn; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication @EnableScheduling public class SpringbootHomeworkEmail0221Application { public static void main(String[] args) { SpringApplication.run(SpringbootHomeworkEmail0221Application.class, args); } }
注意:郵件發(fā)多了,可能會導致qq郵箱認為是垃圾郵件,就會出現報錯,所以盡量不要進行郵箱轟炸
到此這篇關于SpringBoot三種方法實現定時發(fā)送郵件的案例的文章就介紹到這了,更多相關SpringBoot定時發(fā)送郵件內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
spring boot項目沒有mainClass如何實現打包運行
這篇文章主要介紹了spring boot項目沒有mainClass如何實現打包運行,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01解決SpringCloud Feign傳對象參數調用失敗的問題
這篇文章主要介紹了解決SpringCloud Feign傳對象參數調用失敗的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06Springboot-Starter造輪子之自動鎖組件lock-starter實現
這篇文章主要為大家介紹了Springboot-Starter造輪子之自動鎖組件lock-starter實現詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-05-05SpringBoot+ThreadLocal+AbstractRoutingDataSource實現動態(tài)切換數據源
最近在做業(yè)務需求時,需要從不同的數據庫中獲取數據然后寫入到當前數據庫中,因此涉及到切換數據源問題,所以本文采用ThreadLocal+AbstractRoutingDataSource來模擬實現dynamic-datasource-spring-boot-starter中線程數據源切換,需要的朋友可以參考下2023-08-08Springmvc調用存儲過程,并返回存儲過程返還的數據方式
這篇文章主要介紹了Springmvc調用存儲過程,并返回存儲過程返還的數據方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11