SpringBoot任務之詳解郵件任務
01: 異步任務
02: 定時任務
一、SpringBoot–任務:郵件任務
1.1 添加依賴(增加郵件支持)
pom.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
1.2 獲取第三方授權碼
1.2.1 登錄QQ郵箱
1.2.2 點擊設置
1.2.3 切換到賬戶
1.2.4 開啟服務
1.2.5 驗證密保
1.2.6 成功獲取授權碼
這是為瞭不直接暴露QQ密碼設置的機制
ljoghujwhbkpbaff
1.2.7 保存更改(左下角)
1.3 配置郵件信息
application.properties
# qq郵箱 [email protected] # 剛剛生成的授權碼 spring.mail.password=ljoghujwhbkpbaff # qq郵箱的host spring.mail.host=smtp.qq.com #開啟加密驗證(qq郵箱) spring.mail.properties.mail.smtp.ssl.enable=true
1.4 簡單郵件發送
直接調用SpringBoot
的JavaMailSenderImpl
類,使用SimpleMailMessage
發送簡單郵件
EmaildemoApplicationTests.java
package com.tian.emaildemo; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSenderImpl; @SpringBootTest class EmaildemoApplicationTests { @Autowired JavaMailSenderImpl mailSender; @Test void contextLoads() { SimpleMailMessage simpleMailMessage = new SimpleMailMessage(); // 郵件主題 simpleMailMessage.setSubject("你好"); // 郵件文本 simpleMailMessage.setText("Hello world"); // 接收方 simpleMailMessage.setTo("[email protected]"); // 發送方 simpleMailMessage.setFrom("[email protected]"); // 發送郵件 mailSender.send(simpleMailMessage); } }
運行結果:
1.5 復雜郵件發送
調用mailSender.createMimeMessage()
並使用MimeMessageHelper
配置郵件內容,發送即可,郵件內容後設置為true
可以解析html
格式的內容
EmaildemoApplicationTests.java
@Test void contextLoads() throws MessagingException { SimpleMailMessage simpleMailMessage = new SimpleMailMessage(); //復雜郵件 MimeMessage mimeMessage = mailSender.createMimeMessage(); //組裝 MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true); mimeMessageHelper.setSubject("復雜郵件"); // 發送文本: html文本 mimeMessageHelper.setText("<p style='color: orange;'>\n" + " 海客談瀛洲,煙濤微茫信難求;<br>\n" + "\n" + " 越人語天姥,雲霞明滅或可睹。<br>\n" + "\n" + " 天姥連天向天橫,勢拔五嶽掩赤城。<br>\n" + "\n" + " 天臺四萬八千丈,對此欲倒東南傾。<br>\n" + "\n" + " (四萬 一作:一萬)我欲因之夢吳越,一夜飛度鏡湖月。<br>\n" + "\n" + " (度 通:渡)湖月照我影,送我至剡溪。<br>\n" + "\n" + " 謝公宿處今尚在,淥水蕩漾清猿啼。<br>\n" + " <br>\n" + " 腳著謝公屐,身登青雲梯。<br>\n" + "\n" + " 半壁見海日,空中聞天雞。<br>\n" + "\n" + " 千巖萬轉路不定,迷花倚石忽已暝。<br>\n" + "\n" + " 熊咆龍吟殷巖泉,栗深林兮驚層巔。<br>\n" + "\n" + " 雲青青兮欲雨,水澹澹兮生煙。<br>\n" + "\n" + " 列缺霹靂,丘巒崩摧。<br>\n" + " <br>\n" + " 洞天石扉,訇然中開。<br>\n" + "\n" + " 青冥浩蕩不見底,日月照耀金銀臺。<br>\n" + "\n" + " 霓為衣兮風為馬,雲之君兮紛紛而來下。<br>\n" + "\n" + " 虎鼓瑟兮鸞回車,仙之人兮列如麻。<br>\n" + "\n" + " 忽魂悸以魄動,恍驚起而長嗟。<br>\n" + " <br>\n" + " 惟覺時之枕席,失向來之煙霞。<br>\n" + "\n" + " 世間行樂亦如此,古來萬事東流水。<br>\n" + "\n" + " 別君去兮何時還?<br>\n" + "\n" + " 且放白鹿青崖間。<br>\n" + "\n" + " 須行即騎訪名山。<br>\n" + "\n" + " 安能摧眉折腰事權貴,使我不得開心顏!<br>\n" + " </p>", true);//true,開啟html解析 // 發送圖片 mimeMessageHelper.addAttachment("重慶理工大學夜景.jpg", new File("D:\\圖片\\schoolNight.jpg")); mimeMessageHelper.setTo("[email protected]"); mimeMessageHelper.setFrom("[email protected]"); mailSender.send(mimeMessage); }
運行結果:
我不知道為什麼:我寫的CSS樣式在電腦上面不可預見,但是在我的手機QQ郵箱確是可以看見的。歡迎知道的大神告訴我
1.6 可以把上面復雜發送郵件封裝成一個方法
public void SendMail(Boolean html, String title, String text, File file, String sendTo, String sendFrom) throws MessagingException { //復雜郵件 MimeMessage mimeMessage = mailSender.createMimeMessage(); //組裝 MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true); mimeMessageHelper.setSubject(title); mimeMessageHelper.setText(text, html);//true,開啟html解析 mimeMessageHelper.addAttachment("1.jpg", file); mimeMessageHelper.setTo(sendTo); mimeMessageHelper.setFrom(sendFrom); mailSender.send(mimeMessage); }
到此這篇關於SpringBoot任務之詳解郵件任務的文章就介紹到這瞭,更多相關SpringBoot郵件任務內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- Spring Boot實現發送郵件
- Springboot實現發送郵件
- 詳解Springboot中的異步、定時、郵件任務
- SpringBoot實現郵件發送功能的姿勢分享
- SpringBoot Mail郵件任務詳情