springboot整合redis實(shí)現(xiàn)發(fā)送郵箱并驗(yàn)證
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è)置端口號(hào) 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ù)器連接密碼(默認(rèn)為空) password: XXX jedis: pool: #連接池最大阻塞等待時(shí)間(使用負(fù)值表示沒(méi)有限制) max-wait: 1000 #連接池最大連接數(shù)(使用負(fù)值表示沒(méi)有限制) max-active: 100 #連接池中的最大空閑連接 max-idle: 20 #連接池中的最小空閑連接 min-idle: 0 #連接超時(shí)時(shí)間(毫秒) 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ā)送郵件錯(cuò)誤:",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ā)送驗(yàn)證碼 redis存儲(chǔ)驗(yàn)證碼 * @param to 被發(fā)送的郵箱賬號(hào) * @return */ @PostMapping("/fasong") @ResponseBody public String email(String to) { try { //生成6位隨機(jī)數(shù) String i = String.valueOf((int) ((Math.random() * 9 + 1) * 100000)); //發(fā)送郵箱 mailServiceUtils.sendMail("XXXXXX@qq.com", to, "驗(yàn)證碼", i); //redis保存驗(yàn)證碼 redisTemplate.opsForValue().set(to, i); } catch (Exception e) { return "報(bào)錯(cuò)"; } return "OK"; } /** * 郵箱驗(yàn)證 * @param to 被發(fā)送的郵箱賬號(hào) * @param yzm 輸入的驗(yàn)證碼判斷 * @return */ @PostMapping("/yz") @ResponseBody public String yz(String to, String yzm) { //根據(jù)郵箱帳號(hào)取出驗(yàn)證碼 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> 接收方郵箱號(hào) <input type="text" id="to"> <input type="button" value="發(fā)送驗(yàn)證碼" id="yzm"> 驗(yàn)證碼<input type="text" id="yz"> <input type="submit" value="驗(yàn)證" id="y"> </div> <script type="text/javascript" src="../static/jquery-1.8.3.js"></script> <script> /** * 發(fā)送驗(yàn)證碼 */ $("#yzm").click(function() { $.ajax({ url : "/fasong", type : "post", data : { "to":$("#to").val() }, success : function(data) { alert(data) } }) }) /** * 驗(yàn)證碼判斷 */ $("#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實(shí)現(xiàn)發(fā)送郵箱并驗(yàn)證的文章就介紹到這了,更多相關(guān)springboot redis發(fā)送郵箱內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot整合Redis之編寫RedisConfig
- 使用SpringBoot中整合Redis
- SpringBoot整合Redis將對(duì)象寫入redis的實(shí)現(xiàn)
- Spring?boot?整合?Redisson實(shí)現(xiàn)分布式鎖并驗(yàn)證功能
- SpringBoot整合Shiro和Redis的示例代碼
- spring boot整合redis主從sentinel方式
- SpringBoot整合Mysql和Redis的詳細(xì)過(guò)程
- SpringBoot整合Redis實(shí)現(xiàn)訪問(wèn)量統(tǒng)計(jì)的示例代碼
- Spring Boot示例代碼整合Redis詳解
相關(guān)文章
springboot動(dòng)態(tài)調(diào)整日志級(jí)別的操作大全
這篇文章主要介紹了springboot動(dòng)態(tài)調(diào)整日志級(jí)別的方法,本文通過(guò)實(shí)例圖文相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-10-10java文件的簡(jiǎn)單讀寫操作方法實(shí)例分析
這篇文章主要介紹了java文件的簡(jiǎn)單讀寫操作方法,結(jié)合實(shí)例形式分析了java文件流進(jìn)行讀寫操作的方法與相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2020-05-05Java通過(guò)Fork/Join優(yōu)化并行計(jì)算
這篇文章主要為大家詳細(xì)介紹了Java通過(guò)Fork、Join來(lái)優(yōu)化并行計(jì)算,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04SpringBoot實(shí)現(xiàn)動(dòng)態(tài)控制定時(shí)任務(wù)支持多參數(shù)功能
這篇文章主要介紹了SpringBoot實(shí)現(xiàn)動(dòng)態(tài)控制定時(shí)任務(wù)-支持多參數(shù)功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-05-05Java非法字符: ‘\ufeff‘問(wèn)題及說(shuō)明
這篇文章主要介紹了Java非法字符: ‘\ufeff‘問(wèn)題及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02Spring RedirectAttributes參數(shù)跳轉(zhuǎn)代碼實(shí)例
這篇文章主要介紹了Spring RedirectAttributes參數(shù)跳轉(zhuǎn)代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04mybatis-plus saveOrUpdateBatch踩坑記錄
這篇文章主要介紹了mybatis-plus saveOrUpdateBatch踩坑記錄,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12解析MyBatisPlus解決邏輯刪除與唯一索引的兼容問(wèn)題
這篇文章主要介紹了MyBatisPlus解決邏輯刪除與唯一索引的兼容問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04