1. 引入spring-boot-starter-mail 依賴包
1
2
3
4
|
< dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-mail</ artifactId > </ dependency > |
2. 在application.yml配置郵箱基本信息
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
spring: mail: host: smtp.exmail.qq.com port: 465 username: xxx@xx.com password: xxxx protocol: smtp properties: mail: smtp: auth: true ssl: enable: true socketFactory: class: com.sun.mail.util.MailSSLSocketFactory fallback: false |
3. 實現(xiàn)代碼
1
2
3
4
5
6
7
8
9
10
11
|
@Autowired JavaMailSender javaMailSender; public void testSend() { SimpleMailMessage message = new SimpleMailMessage(); message.setFrom( "XXX@xxx.com" ); //發(fā)送者郵箱地址 此地址一定要和yml郵箱一致 message.setTo( "xxx@xxx.com" ); //收件人郵箱地址 message.setSubject( "測試主題" ); message.setText( "測試內(nèi)容" ); jms.send(message); } |
注意:
如果代碼報:501 mail from address must be same as authorization user 錯誤 ;引起原因是yml中配置的郵箱地址和代碼中message.setFrom(xx@xx.com);不一致導(dǎo)致;
到此這篇關(guān)于SpringBoot+JavaMailSender實現(xiàn)騰訊企業(yè)郵箱配置的文章就介紹到這了,更多相關(guān)SpringBoot JavaMailSender騰訊郵箱配置內(nèi)容請搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://blog.csdn.net/qq_34287953/article/details/115671687