千家信息网

如何使用springboot整合redis实现发送邮箱并验证

发表于:2024-09-22 作者:千家信息网编辑
千家信息网最后更新 2024年09月22日,这篇文章主要为大家展示了"如何使用springboot整合redis实现发送邮箱并验证",内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下"如何使用springb
千家信息网最后更新 2024年09月22日如何使用springboot整合redis实现发送邮箱并验证

这篇文章主要为大家展示了"如何使用springboot整合redis实现发送邮箱并验证",内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下"如何使用springboot整合redis实现发送邮箱并验证"这篇文章吧。

1.起步

pom文件

                      org.springframework.boot            spring-boot-starter-redis            1.4.1.RELEASE                                    org.springframework.boot            spring-boot-starter-mail        

下面是yml配置

#设置端口号server:  port: 8080#配置数据源spring:  mail:    #QQ邮箱这不用改    host: smtp.qq.com    #你的邮箱    username: XX@qq.com    #你的授权码    password: XXXXXX    default-encoding: UTF-8  redis:    #redis服务器地址    host: XXXXXX    #Redis服务器连接端口    port: 6379    #Redis服务器连接密码(默认为空)    password: XXX    jedis:      pool:        #连接池最大阻塞等待时间(使用负值表示没有限制)        max-wait: 1000        #连接池最大连接数(使用负值表示没有限制)        max-active: 100        #连接池中的最大空闲连接        max-idle: 20        #连接池中的最小空闲连接        min-idle: 0        #连接超时时间(毫秒)    timeout: 30000
邮箱授权码不知道的话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;@Componentpublic class MailServiceUtils{    private final Logger logger = LoggerFactory.getLogger(this.getClass());    @Autowired    private JavaMailSender mailSender;    /**     * @param from 发送人     * @param to 接收人     * @param subject 主题     * @param content 内容     */    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("邮件成功发送!");        } catch (MailException e) {            logger.error("发送邮件错误:",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 **/@Configurationpublic class Redisconfig {    @Bean(name="redisTemplate")    public RedisTemplate redisTemplate(RedisConnectionFactory factory) {        RedisTemplate template = new RedisTemplate<>();        RedisSerializer 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  邮箱发送 * @Author 86176 * @Date 2021-12-17 15:28 * @Version 1.0 **/@Controllerpublic class EmailController {    @Resource    private MailServiceUtils mailServiceUtils;    @Resource    private RedisTemplate redisTemplate;    /**     * 发送验证码 redis存储验证码     * @param to 被发送的邮箱账号     * @return     */    @PostMapping("/fasong")    @ResponseBody    public String email(String to) {        try {            //生成6位随机数            String i = String.valueOf((int) ((Math.random() * 9 + 1) * 100000));            //发送邮箱            mailServiceUtils.sendMail("XXXXXX@qq.com", to, "验证码", i);            //redis保存验证码            redisTemplate.opsForValue().set(to, i);        } catch (Exception e) {            return "报错";        }        return "OK";    }    /**     * 邮箱验证     * @param to 被发送的邮箱账号     * @param yzm 输入的验证码判断     * @return     */    @PostMapping("/yz")    @ResponseBody    public String yz(String to, String yzm) {        //根据邮箱帐号取出验证码        String o = (String) redisTemplate.opsForValue().get(to);        if (o.equals(yzm)){            return "OK";        }        return "No";    }    @RequestMapping("/abc")    public String abc() {        return "QQemail";    }}

4.前端搭建

        Title     
接收方邮箱号 验证码

结果

以上是"如何使用springboot整合redis实现发送邮箱并验证"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!

邮箱 验证 内容 序列 整合 最大 服务器 篇文章 服务 工具 时间 空闲 负值 账号 邮件 学习 帮助 配置 限制 最小 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 excel数据库连接比较卡 网络安全标准论坛举办 网络安全领域的 连锁门店软件开发与设计 英文数据库的主要功能 软件开发公司服务于哪些行业 在关系数据库中表之间的关系 数据库在线设计工具 mac 软件开发论坛 制造执行系统软件开发收费明细 软件开发女生毕业后该怎么发展 远光财务软件开发产品 ip访问服务器 联想服务器换主硬盘 网络安全服务器flag 明月庄主我的世界神奇宝贝服务器 常见的nosql数据库的四种 著名的医学英文数据库有哪些 互联网软件开发模式有哪些 linux服务器日志查看 校园网络安全及其防护策略 旌德自动化软件开发服务设备 深圳道町互联网科技员工经历 河北特色软件开发价格比较 虚拟网络技术在计算机中代表什么 加强网络安全管理工作总结 数据库远程备份数据库 有些电脑无法登录群晖服务器 奥维企业服务器需要收费吗 数据库内网传输数据库
0