您好,登錄后才能下訂單哦!
這篇文章主要介紹SpringBoot任務之郵件任務的示例分析,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
01: 異步任務
02: 定時任務
pom.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
這是為了不直接暴露QQ密碼設置的機制
ljoghujwhbkpbaff
application.properties
# qq郵箱 spring.mail.username=1585526224@qq.com # 剛剛生成的授權碼 spring.mail.password=ljoghujwhbkpbaff # qq郵箱的host spring.mail.host=smtp.qq.com #開啟加密驗證(qq郵箱) spring.mail.properties.mail.smtp.ssl.enable=true
直接調用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("1585526224@qq.com"); // 發送方 simpleMailMessage.setFrom("1585526224@qq.com"); // 發送郵件 mailSender.send(simpleMailMessage); } }
運行結果:
調用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("1585526224@qq.com"); mimeMessageHelper.setFrom("1585526224@qq.com"); mailSender.send(mimeMessage); }
運行結果:
我不知道為什么:我寫的CSS樣式在電腦上面不可預見,但是在我的手機QQ郵箱確是可以看見的。歡迎知道的大神告訴我
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任務之郵件任務的示例分析”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。