springboot整合redis实现发送邮箱并验证

目录

  • 1.起步
  • 2.工具类
    • 邮箱工具类
    • redis乱码解决
  • 3.controller搭建
    • 4.前端搭建
      • 结果
        • 总结

          1.起步
          pom文件
          org.springframework.bootspring-boot-starter-redis1.4.1.RELEASEorg.springframework.bootspring-boot-starter-mail

          下面是yml配置
          #设置端口号server:port: 8080#配置数据源spring:mail:#QQ邮箱这不用改host: smtp.qq.com#你的邮箱username: XX@qq.com#你的授权码password: XXXXXXdefault-encoding: UTF-8redis:#redis服务器地址host: XXXXXX#Redis服务器连接端口port: 6379#Redis服务器连接密码(默认为空)password: XXXjedis:pool:#连接池最大阻塞等待时间(使用负值表示没有限制)max-wait: 1000#连接池最大连接数(使用负值表示没有限制)max-active: 100#连接池中的最大空闲连接max-idle: 20#连接池中的最小空闲连接min-idle: 0#连接超时时间(毫秒)timeout: 30000

          邮箱授权码不知道的话QQ邮箱开通一下

          springboot整合redis实现发送邮箱并验证
          文章图片


          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()); @Autowiredprivate 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搭建
          【springboot整合redis实现发送邮箱并验证】按要求更改
          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 {@Resourceprivate MailServiceUtils mailServiceUtils; @Resourceprivate RedisTemplate redisTemplate; /*** 发送验证码 redis存储验证码* @param to 被发送的邮箱账号* @return*/@PostMapping("/fasong")@ResponseBodypublic 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")@ResponseBodypublic 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实现发送邮箱并验证
          文章图片

          springboot整合redis实现发送邮箱并验证
          文章图片


          总结 到此这篇关于springboot整合redis实现发送邮箱并验证的文章就介绍到这了,更多相关springboot redis发送邮箱内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

            推荐阅读