Spring Boot實(shí)現(xiàn)郵件發(fā)送功能
本文實(shí)例為大家分享了Spring Boot郵件發(fā)送功能的具體代碼,供大家參考,具體內(nèi)容如下
1、引入依賴
<!-- mail依賴 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
2、參數(shù)配置
在application.properties中配置郵件相關(guān)的參數(shù)
spring.thymeleaf.cache=false spring.mail.host=smtp.qq.com spring.mail.username=***@qq.com spring.mail.password=ymwrdffauajebgde //此處的密碼時(shí)qq郵箱的授權(quán)碼 spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=true spring.mail.properties.mail.smtp.stattls.required=true
3、郵件Service代碼
@Service public class MailService { @Value("${spring.mail.username}") private String from; @Autowired private JavaMailSender sender; /*發(fā)送郵件的方法*/ public void sendSimple(String to, String title, String content){ SimpleMailMessage message = new SimpleMailMessage(); message.setFrom(from); //發(fā)送者 message.setTo(to); //接受者 message.setSubject(title); //發(fā)送標(biāo)題 message.setText(content); //發(fā)送內(nèi)容 sender.send(message); System.out.println("郵件發(fā)送成功"); } }
4、編寫頁(yè)面代碼
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"> <head> <meta charset="UTF-8" /> <title>Insert title here</title> </head> <body> <h1 th:inlines="text">郵件發(fā)送</h1> <form action="sendMail" method="post"> <p>選擇文件: <input type="text" name="title"/></p> <p><input type="submit" value="提交"/></p> </form> </body> </html>
5、郵件請(qǐng)求處理
@Controller public class MailController { @Autowired private MailService mailService; private String to="***@qq.com"; @RequestMapping("mail") public String mail(){ return "/mail"; } @RequestMapping("sendMail") @ResponseBody public String sendMail(@RequestParam("title")String title){ System.out.println("-----title: " + title); mailService.sendSimple(to, title, title); return "success"; } }
6、測(cè)試
7、qq郵箱授權(quán)碼
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- SpringBoot使用FreeMarker模板發(fā)送郵件
- SpringBoot集成E-mail發(fā)送各種類型郵件
- SpringBoot實(shí)現(xiàn)發(fā)送郵件功能
- SpringBoot發(fā)送郵件功能 驗(yàn)證碼5分鐘過期
- 基于SpringBoot實(shí)現(xiàn)發(fā)送帶附件的郵件
- Spring Boot整合郵件發(fā)送與注意事項(xiàng)
- Spring Boot中利用JavaMailSender發(fā)送郵件的方法示例(附源碼)
- Spring Boot實(shí)戰(zhàn)之發(fā)送郵件示例代碼
- Springboot實(shí)現(xiàn)郵件發(fā)送功能
- SpringBoot實(shí)現(xiàn)郵件發(fā)送功能的姿勢(shì)分享
相關(guān)文章
Mybatis pagehelper分頁(yè)插件使用過程解析
這篇文章主要介紹了mybatis pagehelper分頁(yè)插件使用過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02SpringMVC 向jsp頁(yè)面?zhèn)鬟f數(shù)據(jù)庫(kù)讀取到的值方法
下面小編就為大家分享一篇SpringMVC 向jsp頁(yè)面?zhèn)鬟f數(shù)據(jù)庫(kù)讀取到的值方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-03-03Java利用樸素貝葉斯分類算法實(shí)現(xiàn)信息分類
貝葉斯分類算法是統(tǒng)計(jì)學(xué)的一種分類方法,它是一類利用概率統(tǒng)計(jì)知識(shí)進(jìn)行分類的算法。本文將利用樸素貝葉斯分類算法實(shí)現(xiàn)信息分類,需要的可以參考一下2022-06-06maven-maven使用-P參數(shù)打包不同環(huán)境問題
這篇文章主要介紹了maven-maven使用-P參數(shù)打包不同環(huán)境問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11詳解SpringCloudGateway內(nèi)存泄漏問題
這篇文章主要介紹了詳解SpringCloudGateway內(nèi)存泄漏問題,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07