Java登陆注册之验证码的发送

  • 最近没事学了点前后端的东西!(不是不打题QAQ…)
    上个周末搞了搞了注册登陆,emmmmm,前端页面也自己写了写!虽说有点不好看,凑合用用吧!

在这里插入图片描述
在这里插入图片描述
所以搞了个邮件验证码!参考了一些博客:
1. https://blog.csdn.net/j18423532754/article/details/80324700
2. https://www.cnblogs.com/xyzq/p/5704358.html

  • 还行搞了一个中午总算成功了!!上面两篇博客都讲的蛮详细的!对了还需要3个jar包,一定得附加上去!
/*邮件验证码*/
/*对此部分进行封装一下比较好,主要现在自己先练练手,*/
   @RequestMapping("/sendemailcode")
   @ResponseBody
   public String sendemailcode(HttpServletRequest req, String userEmail){
       try{
           HtmlEmail email = new HtmlEmail();
           email.setHostName("smtp.qq.com");
           email.setCharset("utf-8");
           email.addTo(userEmail);
           email.setFrom("[email protected]");
           email.setAuthentication("[email protected]", "wfarmklyqroycbad");
           email.setSubject("验证码");
           String randCode = getRandNum();
           email.setMsg("<h1>您的验证码为: " + randCode +"</h1>");
           email.send();
           //  写入session
           req.getSession().setAttribute(userEmail, randCode);

       }catch (Exception e){
           req.getSession().setAttribute(userEmail, "邮件发送失败");
           return "false";
       }
       return "success";
   }

   public String getRandNum(){
       String randCode = String.valueOf((int)(Math.random() * 9999));
       while(randCode.length() < 4) randCode = "0" + randCode;
       return randCode;
   }
  • 最后引用一下营长的话!
    在这里插入图片描述

最后推荐一个博客: https://hanweizhe.learn-code.cn

猜你喜欢

转载自blog.csdn.net/HKer_YM/article/details/92585911
今日推荐