Spring Boot實現(xiàn)郵件發(fā)送功能
更新時間:2017年06月14日 11:45:47 作者:Miss_wang
這篇文章主要為大家詳細介紹了Spring Boot實現(xiàn)郵件發(fā)送功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Spring Boot郵件發(fā)送功能的具體代碼,供大家參考,具體內(nèi)容如下
1、引入依賴
<!-- mail依賴 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
2、參數(shù)配置
在application.properties中配置郵件相關的參數(shù)
spring.thymeleaf.cache=false spring.mail.host=smtp.qq.com spring.mail.username=***@qq.com spring.mail.password=ymwrdffauajebgde //此處的密碼時qq郵箱的授權碼 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ā)送標題
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郵箱授權碼


以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- SpringBoot使用FreeMarker模板發(fā)送郵件
- SpringBoot集成E-mail發(fā)送各種類型郵件
- SpringBoot實現(xiàn)發(fā)送郵件功能
- SpringBoot發(fā)送郵件功能 驗證碼5分鐘過期
- 基于SpringBoot實現(xiàn)發(fā)送帶附件的郵件
- Spring Boot整合郵件發(fā)送與注意事項
- Spring Boot中利用JavaMailSender發(fā)送郵件的方法示例(附源碼)
- Spring Boot實戰(zhàn)之發(fā)送郵件示例代碼
- Springboot實現(xiàn)郵件發(fā)送功能
- SpringBoot實現(xiàn)郵件發(fā)送功能的姿勢分享
相關文章
springboot整合shiro實現(xiàn)記住我功能
這篇文章主要介紹了springboot整合shiro實現(xiàn)記住我功能,配置類 ShiroConfig,通過實例代碼給大家介紹的非常詳細,需要的朋友可以參考下2021-10-10
Java隊列篇之實現(xiàn)數(shù)組模擬隊列及可復用環(huán)形隊列詳解
像棧一樣,隊列(queue)也是一種線性表,它的特性是先進先出,插入在一端,刪除在另一端。就像排隊一樣,剛來的人入隊(push)要排在隊尾(rear),每次出隊(pop)的都是隊首(front)的人2021-10-10
詳解SpringSecurity如何實現(xiàn)前后端分離
這篇文章主要為大家介紹了詳解SpringSecurity如何實現(xiàn)前后端分離,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-03-03
Spring實戰(zhàn)之獲取其他Bean的屬性值操作示例
這篇文章主要介紹了Spring實戰(zhàn)之獲取其他Bean的屬性值操作,結合實例形式分析了Spring操作Bean屬性值的相關配置與實現(xiàn)技巧,需要的朋友可以參考下2019-12-12

