经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » Redis » 查看文章
springboot整合redis实现发送邮箱并验证
来源:jb51  时间:2022/1/2 12:22:14  对本文有异议

1.起步

pom文件

  1. <!--集成redis-->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-redis</artifactId>
  5. <version>1.4.1.RELEASE</version>
  6. </dependency>
  7. <!--邮箱-->
  8. <dependency>
  9. <groupId>org.springframework.boot</groupId>
  10. <artifactId>spring-boot-starter-mail</artifactId>
  11. </dependency>

下面是yml配置

  1. #设置端口号
  2. server:
  3. port: 8080
  4. #配置数据源
  5. spring:
  6. mail:
  7. #QQ邮箱这不用改
  8. host: smtp.qq.com
  9. #你的邮箱
  10. username: XX@qq.com
  11. #你的授权码
  12. password: XXXXXX
  13. default-encoding: UTF-8
  14. redis:
  15. #redis服务器地址
  16. host: XXXXXX
  17. #Redis服务器连接端口
  18. port: 6379
  19. #Redis服务器连接密码(默认为空)
  20. password: XXX
  21. jedis:
  22. pool:
  23. #连接池最大阻塞等待时间(使用负值表示没有限制)
  24. max-wait: 1000
  25. #连接池最大连接数(使用负值表示没有限制)
  26. max-active: 100
  27. #连接池中的最大空闲连接
  28. max-idle: 20
  29. #连接池中的最小空闲连接
  30. min-idle: 0
  31. #连接超时时间(毫秒)
  32. timeout: 30000
  1. 邮箱授权码不知道的话QQ邮箱开通一下

在这里插入图片描述

2.工具类

邮箱工具类

  1. package com.example.demo.util;
  2.  
  3. /**
  4. * @Classname MailServiceUtils
  5. * @Description TODO
  6. * @Author 86176
  7. * @Date 2021-12-17 15:04
  8. * @Version 1.0
  9. **/
  10. import org.slf4j.Logger;
  11. import org.slf4j.LoggerFactory;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.mail.MailException;
  14. import org.springframework.mail.SimpleMailMessage;
  15. import org.springframework.mail.javamail.JavaMailSender;
  16. import org.springframework.stereotype.Component;
  17. @Component
  18. public class MailServiceUtils{
  19. private final Logger logger = LoggerFactory.getLogger(this.getClass());
  20. @Autowired
  21. private JavaMailSender mailSender;
  22. /**
  23. * @param from 发送人
  24. * @param to 接收人
  25. * @param subject 主题
  26. * @param content 内容
  27. */
  28. public void sendMail(String from,String to, String subject, String content){
  29. SimpleMailMessage message = new SimpleMailMessage();
  30. message.setFrom(from);
  31. message.setTo(to);
  32. message.setSubject(subject);
  33. message.setText(content);
  34. try {
  35. mailSender.send(message); logger.info("邮件成功发送!");
  36. } catch (MailException e) {
  37. logger.error("发送邮件错误:",e);
  38. }
  39. }
  40. }
  41.  

redis乱码解决

  1. package com.example.demo.config;
  2.  
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.data.redis.connection.RedisConnectionFactory;
  6. import org.springframework.data.redis.core.RedisTemplate;
  7. import org.springframework.data.redis.serializer.RedisSerializer;
  8. import org.springframework.data.redis.serializer.StringRedisSerializer;
  9.  
  10. /**
  11. * @Classname Redisconfig
  12. * @Description TODO
  13. * @Author 86176
  14. * @Date 2021-12-06 10:02
  15. * @Version 1.0
  16. **/
  17. @Configuration
  18. public class Redisconfig {
  19.  
  20.  
  21. @Bean(name="redisTemplate")
  22. public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
  23. RedisTemplate<String, String> template = new RedisTemplate<>();
  24. RedisSerializer<String> redisSerializer = new StringRedisSerializer();
  25. template.setConnectionFactory(factory);
  26. //key序列化方式
  27. template.setKeySerializer(redisSerializer);
  28. //value序列化
  29. template.setValueSerializer(redisSerializer);
  30. //value hashmap序列化
  31. template.setHashValueSerializer(redisSerializer);
  32. //key haspmap序列化
  33. template.setHashKeySerializer(redisSerializer);
  34. //
  35. return template;
  36. }
  37. }
  38.  

3.controller搭建

按要求更改

  1. package com.example.demo.controller;
  2.  
  3. import com.example.demo.util.MailServiceUtils;
  4. import org.springframework.data.redis.core.RedisTemplate;
  5. import org.springframework.scheduling.annotation.Async;
  6. import org.springframework.stereotype.Controller;
  7. import org.springframework.util.Assert;
  8. import org.springframework.web.bind.annotation.PostMapping;
  9. import org.springframework.web.bind.annotation.RequestMapping;
  10. import org.springframework.web.bind.annotation.ResponseBody;
  11.  
  12. import javax.annotation.Resource;
  13.  
  14. /**
  15. * @Classname EmailController
  16. * @Description TODO 邮箱发送
  17. * @Author 86176
  18. * @Date 2021-12-17 15:28
  19. * @Version 1.0
  20. **/
  21. @Controller
  22. public class EmailController {
  23.  
  24. @Resource
  25. private MailServiceUtils mailServiceUtils;
  26.  
  27. @Resource
  28. private RedisTemplate<String, Object> redisTemplate;
  29.  
  30. /**
  31. * 发送验证码 redis存储验证码
  32. * @param to 被发送的邮箱账号
  33. * @return
  34. */
  35. @PostMapping("/fasong")
  36. @ResponseBody
  37. public String email(String to) {
  38. try {
  39. //生成6位随机数
  40. String i = String.valueOf((int) ((Math.random() * 9 + 1) * 100000));
  41. //发送邮箱
  42. mailServiceUtils.sendMail("XXXXXX@qq.com", to, "验证码", i);
  43. //redis保存验证码
  44. redisTemplate.opsForValue().set(to, i);
  45. } catch (Exception e) {
  46. return "报错";
  47. }
  48. return "OK";
  49. }
  50.  
  51. /**
  52. * 邮箱验证
  53. * @param to 被发送的邮箱账号
  54. * @param yzm 输入的验证码判断
  55. * @return
  56. */
  57. @PostMapping("/yz")
  58. @ResponseBody
  59. public String yz(String to, String yzm) {
  60. //根据邮箱帐号取出验证码
  61. String o = (String) redisTemplate.opsForValue().get(to);
  62. if (o.equals(yzm)){
  63. return "OK";
  64. }
  65. return "No";
  66. }
  67.  
  68. @RequestMapping("/abc")
  69. public String abc() {
  70. return "QQemail";
  71. }
  72. }
  73.  

4.前端搭建

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. </head>
  7. <body>
  8. <div>
  9. 接收方邮箱号 <input type="text" id="to">
  10. <input type="button" value="发送验证码" id="yzm">
  11. 验证码<input type="text" id="yz">
  12. <input type="submit" value="验证" id="y">
  13. </div>
  14. <script type="text/javascript" src="../static/jquery-1.8.3.js"></script>
  15. <script>
  16. /**
  17. * 发送验证码
  18. */
  19. $("#yzm").click(function() {
  20. $.ajax({
  21. url : "/fasong",
  22. type : "post",
  23. data : {
  24. "to":$("#to").val()
  25. },
  26. success : function(data) {
  27. alert(data)
  28. }
  29. })
  30. })
  31. /**
  32. * 验证码判断
  33. */
  34. $("#y").click(function() {
  35. $.ajax({
  36. url: "/yz",
  37. type: "post",
  38. data: {
  39. "to": $("#to").val(),
  40. "yzm": $("#yz").val()
  41. },
  42. success: function (data) {
  43. alert(data)
  44. }
  45. })
  46. })
  47. </script>
  48. </body>
  49. </html>

结果

redis和邮箱

页面

总结

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

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号