本文實例為大家分享了SpringBoot JavaMailSender發(fā)送郵件的具體代碼,供大家參考,具體內(nèi)容如下
創(chuàng)新互聯(lián)于2013年創(chuàng)立,先為色尼等服務(wù)建站,色尼等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為色尼企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。
引入Maven依賴包
org.springframework.boot spring-boot-starter-mail
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
運行類:
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)題:測試標(biāo)題"); message.setText("測試內(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
運行類:
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)題:測試標(biāo)題"); message.setText("測試內(nèi)容部份"); javaMailSender.send(message); } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。