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 //此處的密碼時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、編寫頁面代碼
<!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、郵件請求處理
@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、測試

7、qq郵箱授權(quán)碼


以上就是本文的全部內(nèi)容,希望對大家的學(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ā)送功能的姿勢分享
相關(guān)文章
springboot整合shiro實(shí)現(xiàn)記住我功能
這篇文章主要介紹了springboot整合shiro實(shí)現(xiàn)記住我功能,配置類 ShiroConfig,通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-10-10
Java語言通過三種方法實(shí)現(xiàn)隊(duì)列的示例代碼
這篇文章主要介紹了Java語言通過三種方法來實(shí)現(xiàn)隊(duì)列的實(shí)例代碼,數(shù)組模擬隊(duì)列,通過對定義的了解,發(fā)現(xiàn)隊(duì)列很像我們的數(shù)組,下面我們通過實(shí)踐給大家詳細(xì)介紹,需要的朋友可以參考下2022-02-02
Java隊(duì)列篇之實(shí)現(xiàn)數(shù)組模擬隊(duì)列及可復(fù)用環(huán)形隊(duì)列詳解
像棧一樣,隊(duì)列(queue)也是一種線性表,它的特性是先進(jìn)先出,插入在一端,刪除在另一端。就像排隊(duì)一樣,剛來的人入隊(duì)(push)要排在隊(duì)尾(rear),每次出隊(duì)(pop)的都是隊(duì)首(front)的人2021-10-10
詳解SpringSecurity如何實(shí)現(xiàn)前后端分離
這篇文章主要為大家介紹了詳解SpringSecurity如何實(shí)現(xiàn)前后端分離,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
Spring實(shí)戰(zhàn)之獲取其他Bean的屬性值操作示例
這篇文章主要介紹了Spring實(shí)戰(zhàn)之獲取其他Bean的屬性值操作,結(jié)合實(shí)例形式分析了Spring操作Bean屬性值的相關(guān)配置與實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-12-12

