springboot整合redis實現(xiàn)發(fā)送郵箱并驗證
更新時間:2022年01月02日 10:52:42 作者:馬牛B
大家好,本篇文章主要講的是springboot整合redis實現(xiàn)發(fā)送郵箱并驗證,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下
1.起步
pom文件
<!--集成redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
<version>1.4.1.RELEASE</version>
</dependency>
<!--郵箱-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
下面是yml配置
#設(shè)置端口號
server:
port: 8080
#配置數(shù)據(jù)源
spring:
mail:
#QQ郵箱這不用改
host: smtp.qq.com
#你的郵箱
username: XX@qq.com
#你的授權(quán)碼
password: XXXXXX
default-encoding: UTF-8
redis:
#redis服務(wù)器地址
host: XXXXXX
#Redis服務(wù)器連接端口
port: 6379
#Redis服務(wù)器連接密碼(默認為空)
password: XXX
jedis:
pool:
#連接池最大阻塞等待時間(使用負值表示沒有限制)
max-wait: 1000
#連接池最大連接數(shù)(使用負值表示沒有限制)
max-active: 100
#連接池中的最大空閑連接
max-idle: 20
#連接池中的最小空閑連接
min-idle: 0
#連接超時時間(毫秒)
timeout: 30000
郵箱授權(quán)碼不知道的話QQ郵箱開通一下

2.工具類
郵箱工具類
package com.example.demo.util;
/**
* @Classname MailServiceUtils
* @Description TODO
* @Author 86176
* @Date 2021-12-17 15:04
* @Version 1.0
**/
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;
@Component
public class MailServiceUtils{
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private JavaMailSender mailSender;
/**
* @param from 發(fā)送人
* @param to 接收人
* @param subject 主題
* @param content 內(nèi)容
*/
public void sendMail(String from,String to, String subject, String content){
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(from);
message.setTo(to);
message.setSubject(subject);
message.setText(content);
try {
mailSender.send(message); logger.info("郵件成功發(fā)送!");
} catch (MailException e) {
logger.error("發(fā)送郵件錯誤:",e);
}
}
}
redis亂碼解決
package com.example.demo.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* @Classname Redisconfig
* @Description TODO
* @Author 86176
* @Date 2021-12-06 10:02
* @Version 1.0
**/
@Configuration
public class Redisconfig {
@Bean(name="redisTemplate")
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, String> template = new RedisTemplate<>();
RedisSerializer<String> redisSerializer = new StringRedisSerializer();
template.setConnectionFactory(factory);
//key序列化方式
template.setKeySerializer(redisSerializer);
//value序列化
template.setValueSerializer(redisSerializer);
//value hashmap序列化
template.setHashValueSerializer(redisSerializer);
//key haspmap序列化
template.setHashKeySerializer(redisSerializer);
//
return template;
}
}
3.controller搭建
按要求更改
package com.example.demo.controller;
import com.example.demo.util.MailServiceUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
/**
* @Classname EmailController
* @Description TODO 郵箱發(fā)送
* @Author 86176
* @Date 2021-12-17 15:28
* @Version 1.0
**/
@Controller
public class EmailController {
@Resource
private MailServiceUtils mailServiceUtils;
@Resource
private RedisTemplate<String, Object> redisTemplate;
/**
* 發(fā)送驗證碼 redis存儲驗證碼
* @param to 被發(fā)送的郵箱賬號
* @return
*/
@PostMapping("/fasong")
@ResponseBody
public String email(String to) {
try {
//生成6位隨機數(shù)
String i = String.valueOf((int) ((Math.random() * 9 + 1) * 100000));
//發(fā)送郵箱
mailServiceUtils.sendMail("XXXXXX@qq.com", to, "驗證碼", i);
//redis保存驗證碼
redisTemplate.opsForValue().set(to, i);
} catch (Exception e) {
return "報錯";
}
return "OK";
}
/**
* 郵箱驗證
* @param to 被發(fā)送的郵箱賬號
* @param yzm 輸入的驗證碼判斷
* @return
*/
@PostMapping("/yz")
@ResponseBody
public String yz(String to, String yzm) {
//根據(jù)郵箱帳號取出驗證碼
String o = (String) redisTemplate.opsForValue().get(to);
if (o.equals(yzm)){
return "OK";
}
return "No";
}
@RequestMapping("/abc")
public String abc() {
return "QQemail";
}
}
4.前端搭建
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div>
接收方郵箱號 <input type="text" id="to">
<input type="button" value="發(fā)送驗證碼" id="yzm">
驗證碼<input type="text" id="yz">
<input type="submit" value="驗證" id="y">
</div>
<script type="text/javascript" src="../static/jquery-1.8.3.js"></script>
<script>
/**
* 發(fā)送驗證碼
*/
$("#yzm").click(function() {
$.ajax({
url : "/fasong",
type : "post",
data : {
"to":$("#to").val()
},
success : function(data) {
alert(data)
}
})
})
/**
* 驗證碼判斷
*/
$("#y").click(function() {
$.ajax({
url: "/yz",
type: "post",
data: {
"to": $("#to").val(),
"yzm": $("#yz").val()
},
success: function (data) {
alert(data)
}
})
})
</script>
</body>
</html>
結(jié)果


總結(jié)
到此這篇關(guān)于springboot整合redis實現(xiàn)發(fā)送郵箱并驗證的文章就介紹到這了,更多相關(guān)springboot redis發(fā)送郵箱內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot結(jié)合mysql主從來實現(xiàn)讀寫分離的方法示例
這篇文章主要介紹了springboot結(jié)合mysql主從來實現(xiàn)讀寫分離的方法示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
Java求s=a+aa+aaa+aaaa+aa...a 5個數(shù)相加的值
求s=a+aa+aaa+aaaa+aa...a的值,其中a是一個數(shù)字。例如2+22+222+2222+22222(此時共有5個數(shù)相加),幾個數(shù)相加有鍵盤控制2017-02-02

