您好,登錄后才能下訂單哦!
本文實(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í)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。