• SpringBoot项目集成发邮件功能


    1:引入依赖

    	<dependency>
    			<groupId>org.apache.commons</groupId>
    			<artifactId>commons-email</artifactId>
    			<version>1.5</version>
    		</dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2:配置设置

    #邮箱配置
    #平台地址,这里用的是qq邮箱,使用其他邮箱请更换
    spring.mail.host = smtp.qq.com
    #改成自己的邮箱
    spring.mail.username = 1055560665@qq.com
    #发送短信后它给你的授权码。填写你自己授权码
    spring.mail.password =********
    spring.mail.properties.mail.smtp.ssl.enable=true
    ##编码格式
    spring.mail.default-encoding=UTF-8
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    3:授权码获取:

    在这里插入图片描述

    4:核心代码

    package com.example.demo.controller;
    import com.sun.org.apache.xml.internal.utils.SerializableLocatorImpl;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.mail.SimpleMailMessage;
    import org.springframework.mail.javamail.JavaMailSender;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import javax.servlet.http.HttpSession;
    import java.io.Serializable;
    import java.util.Random;
    
    @RespController
    @RequestMapping("/email")
    public class emailController implements Serializable {
    
        @Autowired
        private JavaMailSender mailSender;
        @Value("${spring.mail.username}")
        private String myemail;
    
        @RequestMapping("/sendcode")
        public boolean sendMimeMail(String useremail, HttpSession session) {
            try {
                SimpleMailMessage mailMessage = new SimpleMailMessage();
    
                mailMessage.setSubject("博客之站:验证码邮件");//主题
                //生成随机数
                String code = randomCode();
                //这里存在Session再放到redis或者内存;我就能验证了。
    
                  HttpSession session = request.getSession();
                  //不能让别人连续发送
              if(null!=session.getAttribute("code")){
                return Result.fail(-1,"发送过于频繁;请稍后再试");
            }
            //你得在验证码验证后/超过时间后;把Session的code设置为null
                //将随机数放置到session中
                //session.setAttribute("email", useremail);
                session.setAttribute("code", code);
    
                mailMessage.setText("您的验证码是:" + code);//内容
    
                mailMessage.setTo(useremail);//发给谁
    
                mailMessage.setFrom(myemail);//你自己的邮箱
    
                mailSender.send(mailMessage);//发送
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
    
        /**
         * 随机生成6位数的验证码
         *
         * @return String code
         */
        public String randomCode() {
            StringBuilder str = new StringBuilder();
            Random random = new Random();
            for (int i = 0; i < 6; i++) {
                str.append(random.nextInt(10));
            }
            return str.toString();
        }
    
    }
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73

    5:postman模拟验证

    在这里插入图片描述

    6:安全注意

    1:邮箱验证码得设置过期时间。Session的value是可以为null的
    推荐思路:可以开启一个线程;在发送邮箱后阻塞2分半后把Session里的邮箱验证码code设置为null。或者定时器到这个时间后就这些这个任务。

    2:邮箱码验证通过后也得进行清空操作
    也是把Session里的邮箱验证码code设置为null即可。或者是删除掉session.removeAttribute(“code”);

    3:虽然我们能在前端设置按钮只能点击一次邮箱然后恢复使用;但是如果像上述的postman模拟请求那不是能一直给你发邮件了?(写代码这些特殊情况一定得考虑;因为谁的可以给你发请求)
    思路:每一次发送邮件的时候;我们可以取Session的验证码;如果是null我才能发邮件(我们上述操作会把失效/过期验证码这个设置为null)。否则发送失败。

  • 相关阅读:
    OpenCV3-Python(7)模板匹配和霍夫检测
    Week 3 Convolutional Neural Network
    前端JS for循环内异步接口变成同步提交(JavaScript for循环异步变同步)
    LLM生态下爬虫程序的现状与未来
    SpringBoot笔记
    3LU6I是什么二极管
    NLP入门之——Word2Vec词向量Skip-Gram模型代码实现(Pytorch版)
    idea更改java项目名
    【微信小程序】获取用户手机号的实现
    算法通过村第八关-树(深度优先)青铜笔记|经典算法题目
  • 原文地址:https://blog.csdn.net/m0_64254651/article/details/134360912