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中配置郵件相關(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ā)送標題
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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- 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ā)送功能的姿勢分享
相關(guān)文章
SpringMVC 向jsp頁面?zhèn)鬟f數(shù)據(jù)庫讀取到的值方法
下面小編就為大家分享一篇SpringMVC 向jsp頁面?zhèn)鬟f數(shù)據(jù)庫讀取到的值方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03
maven-maven使用-P參數(shù)打包不同環(huán)境問題
這篇文章主要介紹了maven-maven使用-P參數(shù)打包不同環(huán)境問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11
詳解SpringCloudGateway內(nèi)存泄漏問題
這篇文章主要介紹了詳解SpringCloudGateway內(nèi)存泄漏問題,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-07-07

