SpringBoot整合Mail發(fā)送郵件功能
前言
我們在網(wǎng)站上注冊賬號的時(shí)候一般需要獲取驗(yàn)證碼,而這個驗(yàn)證碼一般發(fā)送在你的手機(jī)號上還有的是發(fā)送在你的郵箱中,注冊,賬號密碼…都需要用到驗(yàn)證,今天就演示一下如何用SpringBoot整合Mail發(fā)送郵箱。
Maven依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
使用QQ郵箱演示
國內(nèi)QQ郵箱的用戶占多數(shù),所以本文以QQ郵箱作為演示。
使用QQ郵箱需要開啟SMTP服務(wù)
QQ郵箱默認(rèn)關(guān)閉了SMTP服務(wù),所以需要我們手動打開:

配置application.properties/yml
# 應(yīng)用服務(wù) WEB 訪問端口 server.port=8080 # 郵箱用戶名 spring.mail.username=your mail # 授權(quán)碼 spring.mail.password=授權(quán)碼 # 郵箱主機(jī) spring.mail.host=smtp.qq.com # 開啟SSL spring.mail.properties.mail.smtp.ssl.enable=true # 認(rèn)證 spring.mail.properties.mail.smtp.auth=true # 開啟SSL安全模式 spring.mail.properties.mail.smtp.starttls.enable=true # 必須啟動SSL安全模式 spring.mail.properties.mail.smtp.starttls.required=true # SSL Config # 端口 spring.mail.port=465 # 協(xié)議 spring.mail.protocol=smtp # 默認(rèn)編碼 spring.mail.default-encoding=UTF-8 # 套接字工廠端口 spring.mail.properties.mail.smtp.socketFactory.port=465 # 套接字工廠類 spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
配置好這些后,springboot會自動幫我們配置好相關(guān)的郵件發(fā)送類。
發(fā)送普通郵件
@SpringBootTest
class Demo1ApplicationTests {
@Autowired
JavaMailSender javaMailSender;
@Test
public void sendSimpleMail() {
// 構(gòu)建一個郵件對象
SimpleMailMessage message = new SimpleMailMessage();
// 設(shè)置郵件主題
message.setSubject("這是一封測試郵件");
// 設(shè)置郵件發(fā)送者,這個跟application.yml中設(shè)置的要一致
message.setFrom("1926585708@qq.com");
// 設(shè)置郵件接收者,可以有多個接收者,中間用逗號隔開,以下類似
// message.setTo("1*****@qq.com","2*****qq.com");
message.setTo("1926585708@qq.com");
// 設(shè)置郵件發(fā)送日期
message.setSentDate(new Date());
// 設(shè)置郵件的正文
message.setText("這是測試郵件的正文");
// 發(fā)送郵件
javaMailSender.send(message);
}
}
查看郵箱:

驗(yàn)證碼案例
使用thymeleaf模板搭建HTML頁面
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>驗(yàn)證碼</title>
</head>
<body>
<div style="text-align: center">
請輸入您的郵箱:<input id="email" type="text"> <input id="getcode" type="button" value="獲取驗(yàn)證碼"><br>
驗(yàn)證碼:<input id="code" type="text"><br>
<input id="check" type="button" value="驗(yàn)證"><br>
</div>
<script type="text/javascript" th:src="@{/js/jquery-3.6.0.js}"></script>
<script>
//發(fā)送驗(yàn)證碼
$("#getcode").click(function () {
var btn = $("#getcode");
var time = 30;//定義時(shí)間變量。用于倒計(jì)時(shí)用
var timer = null;//定義一個定時(shí)器;
timer = setInterval(function(){///開啟定時(shí)器。函數(shù)內(nèi)執(zhí)行
btn.disabled = true;
btn.val(time+"秒后重新獲取"); //點(diǎn)擊發(fā)生后,按鈕的文本內(nèi)容變成之前定義好的時(shí)間值。
time--;//時(shí)間值自減
if(time==0){ //判斷,當(dāng)時(shí)間值小于等于0的時(shí)候
btn.val('重新獲取驗(yàn)證碼'); //其文本內(nèi)容變成……點(diǎn)擊重新發(fā)送……
btn.disabled = false;
clearInterval(timer); //清除定時(shí)器
}
},1000)
$.post({
url : "/getmailcode",
data : {"mail":$("#email").val()},
success : function (data) {
alert("驗(yàn)證碼已發(fā)送,請注意查收");
}
});
})
//檢查驗(yàn)證碼
$("#check").click(function (){
$.post({
url: "/checkCode",
data: {"code":$("#code").val()},
success:function (data){
if (data==0){
alert("驗(yàn)證碼錯誤")
}else {
alert("驗(yàn)證碼正確")
}
}
})
})
</script>
</body>
</html>
效果:

定義Controller:
/**
* 郵箱驗(yàn)證碼
*/
@Controller
public class MailCodeController {
@Autowired
MailUtils mailUtils;
/**
* 驗(yàn)證頁面
*
*/
@RequestMapping("/login")
public String login(){
return "login";
}
/**
* 獲取驗(yàn)證碼
* @param mail
* @return
*/
@PostMapping("/getmailcode")
@ResponseBody
public String getMailCode(String mail, HttpSession session){
String mailCode = this.mailUtils.getMailCode(mail);
System.out.println("獲取到驗(yàn)證碼:"+mailCode);
session.setAttribute("code",mailCode);
return "ok";
}
/**
* 檢查驗(yàn)證碼
* @param code
* @return 1:正確 0:錯誤
*/
@PostMapping("/checkCode")
@ResponseBody
public String checkCode(String code, HttpSession session){
String checkCode = (String) session.getAttribute("code");
if (checkCode!=null){
if (code.equals(checkCode)){
return "1";
}else {
return "0";
}
}
return "0";
}
}
MailUtils郵箱工具類:
@Component
public class MailUtils {
@Autowired
private JavaMailSender mailSender;
public String getMailCode(String mail){
Random random = new Random();
String code="";
for (int i=0;i<6;i++)
{
code+=random.nextInt(10);
}
//郵件設(shè)置1:一個簡單的郵件
SimpleMailMessage message = new SimpleMailMessage();
message.setSubject("驗(yàn)證碼消息");
message.setText("您的驗(yàn)證碼為 : 【 "+code+" 】");
message.setTo(mail);
message.setFrom("1926585708@qq.com");
mailSender.send(message);
return code;
}
}
驗(yàn)證效果:

到此這篇關(guān)于SpringBoot整合Mail發(fā)送郵件的文章就介紹到這了,更多相關(guān)SpringBoot發(fā)送郵件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于SqlSessionFactory的openSession方法使用
這篇文章主要介紹了SqlSessionFactory的openSession方法使用,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12
java 中函數(shù)的參數(shù)傳遞詳細(xì)介紹
這篇文章主要介紹了 java 中函數(shù)的參數(shù)傳遞詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下2016-11-11
ThreadLocal使用案例_動力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了ThreadLocal使用案例分析,需要的朋友可以參考下2017-08-08
解決Mybatis返回update后影響的行數(shù)問題
這篇文章主要介紹了解決Mybatis返回update后影響的行數(shù)問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11

