SpringBoot實(shí)現(xiàn)發(fā)送郵件任務(wù)
本文實(shí)例為大家分享了SpringBoot實(shí)現(xiàn)發(fā)送郵件任務(wù)的具體代碼,供大家參考,具體內(nèi)容如下
1.pom中引入spring-boot-starter-mail
<dependencies> <!--郵件--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> <!--web--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
2.假如 張三@qq.com 給 李四@163.com 發(fā)送郵件,張三首先要登錄自己的郵箱,所以先要配置發(fā)送者的賬號(hào)密碼,服務(wù)器地址。
注意:第三方登錄郵箱的時(shí)候,使用的不是郵箱的原本密碼,使用的是臨時(shí)授權(quán)碼。
以QQ郵箱為例子,打開(kāi)郵箱!
點(diǎn)擊賬戶(hù)
往下拉,全部開(kāi)啟,生成授權(quán)碼!
全部開(kāi)啟。點(diǎn)擊生成授權(quán)碼!
3.配置application.properties
##發(fā)件人郵箱 spring.mail.username=119848xxxx@qq.com ##生成的授權(quán)碼 spring.mail.password=lojwzgpnrpzmifgg ##QQ的SMIP地址 spring.mail.host=smtp.qq.com ##配置安全連接 spring.mail.properties.mail.smtp.ssl.enable=true
4.在測(cè)試類(lèi)中測(cè)試
package com.zyb.task; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.JavaMailSenderImpl; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.test.context.junit4.SpringRunner; import javax.mail.internet.MimeMessage; import java.io.File; @RunWith(SpringRunner.class) @SpringBootTest public class SpringbootTaskApplicationTests { //注入郵件發(fā)送器 @Autowired JavaMailSenderImpl javaMailSender; /** * 簡(jiǎn)單郵件測(cè)試 */ @Test public void contextLoads1() { SimpleMailMessage message = new SimpleMailMessage(); message.setText("今晚7點(diǎn)鐘開(kāi)會(huì)"); message.setSubject("通知-開(kāi)會(huì)"); //發(fā)送者郵箱 message.setFrom("119848xxxx@qq.com"); //發(fā)送到哪個(gè)郵箱 message.setTo("zyb_xxx@126.com"); javaMailSender.send(message); } /** * 復(fù)雜郵件測(cè)試 */ @Test public void contextLoads2() throws Exception{ //1.創(chuàng)建一個(gè)復(fù)雜的消息郵件 MimeMessage mimeMessage = javaMailSender.createMimeMessage(); //第二個(gè)參數(shù) 是否需要上傳附件 MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true); //郵件設(shè)置 //這里可以使用html標(biāo)簽樣式 helper.setText("<b style='color:red'>今晚7點(diǎn)鐘開(kāi)會(huì)</b>",true); helper.setSubject("通知-開(kāi)會(huì)"); //發(fā)送者郵箱 helper.setFrom("119848xxxx@qq.com"); //發(fā)送到哪個(gè)郵箱 helper.setTo("zyb_xxx@126.com"); //上傳附件 附件名,路徑 helper.addAttachment("1.jpg",new File("C:\\Users\\Administrator\\Desktop\\img\\iphone壁紙\\1.jpg")); javaMailSender.send(mimeMessage); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
解決使用IDEA時(shí)跳轉(zhuǎn)到.class的問(wèn)題
這篇文章主要介紹了解決使用IDEA時(shí)跳轉(zhuǎn)到.class的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08springboot在filter中如何用threadlocal存放用戶(hù)身份信息
這篇文章主要介紹了springboot中在filter中如何用threadlocal存放用戶(hù)身份信息,本文章主要描述通過(guò)springboot的filter類(lèi),在過(guò)濾器中設(shè)置jwt信息進(jìn)行身份信息保存的方法,需要的朋友可以參考下2024-07-07Java安全之Filter權(quán)限繞過(guò)的實(shí)現(xiàn)
在一些需要挖掘一些無(wú)條件RCE中,大部分類(lèi)似于一些系統(tǒng)大部分地方都做了權(quán)限控制的,而這時(shí)候想要利用權(quán)限繞過(guò)就顯得格外重要,本文就介紹了如何實(shí)現(xiàn),一起來(lái)了解一下2021-05-05maven坐標(biāo)Dependencies和Exclusions的使用
這篇文章主要介紹了maven坐標(biāo)Dependencies和Exclusions的使用,很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12如何使用Spring Security實(shí)現(xiàn)用戶(hù)-角色-資源的權(quán)限控制
文章介紹了如何通過(guò)SpringSecurity實(shí)現(xiàn)用戶(hù)-角色-資源的權(quán)限管理,包括基于角色的請(qǐng)求控制、加載用戶(hù)角色信息、角色與資源的關(guān)聯(lián)等步驟,同時(shí),提供了一些測(cè)試場(chǎng)景,以驗(yàn)證權(quán)限控制是否正確,感興趣的朋友跟隨小編一起看看吧2024-10-10Maven編譯遇到Process terminated問(wèn)題(四種情況全部解決)
這篇文章主要介紹了Maven編譯遇到Process terminated問(wèn)題(四種情況全部解決),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07