springboot實現(xiàn)發(fā)送郵件(QQ郵箱為例)
本文實例為大家分享了springboot實現(xiàn)發(fā)送郵件的具體代碼,供大家參考,具體內(nèi)容如下
1.引入依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
2.找到qq郵箱,開啟smtp服務(wù),這里生成你的密碼,復(fù)制第三步用
3.password里輸入你獲取到的密碼(重點:這里一定不能錯)
spring.mail.username=1550213743@qq.com spring.mail.password=******** spring.mail.host=smtp.qq.com #開啟加密驗證 spring.mail.properties.mail.smtp.ssl.enable=true
4.EmailApplicationTests類里進行測試, contextLoads為簡單的發(fā)送,僅限于文本。contextLoads2為復(fù)雜,可以發(fā)送圖片,html格式
package com.xyj; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.JavaMailSenderImpl; import org.springframework.mail.javamail.MimeMessageHelper; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import java.io.File; @SpringBootTest class EmailApplicationTests { @Autowired JavaMailSenderImpl mailSender; @Test void contextLoads() { SimpleMailMessage mailMessage = new SimpleMailMessage(); mailMessage.setSubject("你好"); mailMessage.setText("123456"); mailMessage.setTo("1550213743@qq.com"); mailMessage.setFrom("1550213743@qq.com"); mailSender.send(mailMessage); } @Test void contextLoads2() throws MessagingException { MimeMessage mimeMessage = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true); helper.setSubject("你好"); helper.setText("<p style='color:red'>這是紅色的</p>",true); helper.addAttachment("bg.jpg",new File("D:\\money\\網(wǎng)頁\\疫情\\images\\bg1.jpg")); helper.setTo("1550213743@qq.com"); helper.setFrom("1550213743@qq.com"); mailSender.send(mimeMessage); } }
5.分別對應(yīng)的結(jié)果
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Maven中dependencyManagement管理項目依賴項
在開發(fā)?Java?項目時,管理和協(xié)調(diào)依賴項的版本號是一項重要而繁瑣的任務(wù),本文主要介紹了Maven中dependencyManagement管理項目依賴項,具有一定的參考價值,感興趣的可以了解一下2024-01-01Springboot web項目打包實現(xiàn)過程解析
這篇文章主要介紹了Springboot web項目打包實現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-08-08Java多線程+鎖機制實現(xiàn)簡單模擬搶票的項目實踐
鎖是一種同步機制,用于控制對共享資源的訪問,在線程獲取到鎖對象后,可以執(zhí)行搶票操作,本文主要介紹了Java多線程+鎖機制實現(xiàn)簡單模擬搶票的項目實踐,具有一定的參考價值,感興趣的可以了解一下2024-02-02SpringBoot之通過BeanPostProcessor動態(tài)注入ID生成器案例詳解
這篇文章主要介紹了SpringBoot之通過BeanPostProcessor動態(tài)注入ID生成器案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-09-09Java實現(xiàn)根據(jù)前端所要格式返回樹形3級層級數(shù)據(jù)
這篇文章主要為大家詳細介紹了Java如何實現(xiàn)根據(jù)前端所要格式返回樹形3級層級數(shù)據(jù),文中的示例代碼講解詳細,有需要的小伙伴可以了解下2024-02-02Java中BigDecimal的equals方法和compareTo方法的區(qū)別詳析
這篇文章主要給大家介紹了關(guān)于Java中BigDecimal的equals方法和compareTo方法區(qū)別的相關(guān)資料,對于BigDecimal的大小比較,用equals方法的話會不僅會比較值的大小,還會比較兩個對象的精確度,而compareTo方法則不會比較精確度,只比較數(shù)值的大小,需要的朋友可以參考下2023-11-11