您好,登錄后才能下訂單哦!
本文實例為大家分享了SpringBoot JavaMailSender發送郵件的具體代碼,供大家參考,具體內容如下
引入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
運行類:
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("標題:測試標題"); message.setText("測試內容部份"); javaMailSender.send(message); } }
QQ郵箱和163郵箱的區別是需要設置授權碼而不是密碼,具體操作參考: 地址
application.properties
######qq郵箱######## spring.mail.host=smtp.qq.com spring.mail.username=******@qq.com #QQ郵箱授權碼 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("標題:測試標題"); message.setText("測試內容部份"); javaMailSender.send(message); } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。