SpringBoot JavaMailSender發(fā)送郵件功能
本文實(shí)例為大家分享了SpringBoot JavaMailSender發(fā)送郵件的具體代碼,供大家參考,具體內(nèi)容如下
引入Maven依賴包
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
163郵箱
application.properties
#####163郵箱######## spring.mail.host=smtp.163.com spring.mail.username=*****@163.com #163郵箱密碼 spring.mail.password=!@#$%^&* spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=true spring.mail.properties.mail.smtp.starttls.required=true
運(yùn)行類:
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(classes=Application.class)
public class My163MailTest {
@Autowired
private JavaMailSender javaMailSender;
@Value("${spring.mail.username}")
private String username;
@Test
public void testSendSimple() {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(username);
message.setTo("*******@qq.com");
message.setSubject("標(biāo)題:測(cè)試標(biāo)題");
message.setText("測(cè)試內(nèi)容部份");
javaMailSender.send(message);
}
}
QQ郵箱和163郵箱的區(qū)別是需要設(shè)置授權(quán)碼而不是密碼,具體操作參考: 地址
application.properties
######qq郵箱######## spring.mail.host=smtp.qq.com spring.mail.username=******@qq.com #QQ郵箱授權(quán)碼 spring.mail.password=xuojxtkdojvzbhjj spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=true spring.mail.properties.mail.smtp.starttls.required=true
運(yùn)行類:
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(classes=Application.class)
public class MyQQMailTest {
@Autowired
private JavaMailSender javaMailSender;
@Value("${spring.mail.username}")
private String username;
@Test
public void testSendSimple() {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(username);
message.setTo("******@qq.com");
message.setSubject("標(biāo)題:測(cè)試標(biāo)題");
message.setText("測(cè)試內(nèi)容部份");
javaMailSender.send(message);
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
IntelliJ IDEA自定義代碼提示模板Live Templates的圖文教程
這篇文章主要介紹了IntelliJ IDEA自定義代碼提示模板Live Templates,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03
java多線程編程之使用Synchronized關(guān)鍵字同步類方法
JAVA中要想解決“臟數(shù)據(jù)”的問題,最簡(jiǎn)單的方法就是使用synchronized關(guān)鍵字來使run方法同步,看下面的代碼,只要在void和public之間加上synchronized關(guān)鍵字2014-01-01
Springboot2.x結(jié)合Mabatis3.x下Hikari連接數(shù)據(jù)庫報(bào)超時(shí)錯(cuò)誤
本文針對(duì)Springboot2.x與Mybatis3.x結(jié)合使用時(shí),Hikari連接數(shù)據(jù)庫出現(xiàn)超時(shí)錯(cuò)誤的問題進(jìn)行了深入分析,并提供了一系列有效的解決方法,感興趣的可以了解一下2023-11-11
基于SpringBoot+Mybatis實(shí)現(xiàn)Mysql分表
這篇文章主要為大家詳細(xì)介紹了基于SpringBoot+Mybatis實(shí)現(xiàn)Mysql分表的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-04-04
java實(shí)現(xiàn)Runnable接口適合資源的共享
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)Runnable接口適合資源的共享,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07
Java通過JNI調(diào)用C++動(dòng)態(tài)庫的完整流程詳解
JNI(Java Native Interface),是實(shí)現(xiàn)Java/Kotlin與C/C++語言之間交互的橋梁,本文主要為大家介紹了Java通過JNI調(diào)用C++動(dòng)態(tài)庫的完整流程,需要的可以參考下2025-04-04
解決feignclient調(diào)用服務(wù),傳遞的中文數(shù)據(jù)成???問題
這篇文章主要介紹了解決feignclient調(diào)用服務(wù),傳遞的中文數(shù)據(jù)成???問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01
java實(shí)現(xiàn)監(jiān)控rtsp流轉(zhuǎn)flv方法實(shí)例(前端播放,前后端代碼都有)
這篇文章主要給大家介紹了關(guān)于java實(shí)現(xiàn)監(jiān)控rtsp流轉(zhuǎn)flv的相關(guān)資料,文中介紹的是前端播放,前后端代碼都有,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-06-06

