发送邮箱验证码

墨蓝 2022-05-29 03:44 460阅读 0赞
  1. /**
  2. * 邮箱验证码
  3. *
  4. * @param userCount 邮箱账号
  5. * @param response
  6. */
  7. @RequestMapping("vcode")
  8. public void vcode(String userCount, HttpServletResponse response) throws IOException {
  9. BackModel result = null;
  10. String title = "账号注册";
  11. try {
  12. if (StringUtils.isEmpty(userCount)) {
  13. result = new BackModel(0, null, "请输入邮箱账号");
  14. } else {
  15. UserInfo userInfo = userInfoService.selectByUserAccount(userCount);
  16. String code = "";
  17. for (int i = 0; i < 6; i++) {
  18. code = code + (int) (Math.random() * 9);
  19. }
  20. SendMailUtils.sendMail(userCount, code, title);
  21. RedisUtils.set(RedisKeys.VCODE + userCount, code, 300);
  22. Date date = new Date();
  23. Calendar cal = Calendar.getInstance();
  24. cal.setTime(date);
  25. long t1 = System.currentTimeMillis();
  26. cal.add(Calendar.DATE, 5);
  27. long t2 = cal.getTimeInMillis();
  28. long t3 = (t2 - t1) / 1000;
  29. RedisUtils.set(RedisKeys.VCODE_RECORD + userCount, "", (int) t3);
  30. result = new BackModel(1, null, "发送成功");
  31. }
  32. } catch (Exception e) {
  33. e.printStackTrace();
  34. result = new BackModel(0, null, "服务器发生异常");
  35. } finally {
  36. response.setContentType("application/json;charset=utf-8");
  37. response.getWriter().write(JSONObject.toJSON(result).toString());
  38. response.getWriter().flush();
  39. response.getWriter().close();
  40. }
  41. }
  42. public class SendMailUtils {
  43. private static String FROM="xxx";// 发件人电子邮箱
  44. private static String VCode="1234567";//授权码或者账号密码
  45. // 方法一:必须要用授权码
  46. public static void sendMail(String email,String code,String title){
  47. // 1.创建连接对象javax.mail.Session
  48. // 2.创建邮件对象 javax.mail.Message
  49. // 3.发送一封激活邮件
  50. String host = "smtp.qq.com"; // 指定发送邮件的主机smtp.qq.com(QQ)|smtp.163.com(网易)
  51. Properties properties = System.getProperties();// 获取系统属性
  52. properties.setProperty("mail.smtp.host", host);// 设置邮件服务器
  53. properties.setProperty("mail.smtp.auth", "true");// 打开认证
  54. try {
  55. //QQ邮箱需要下面这段代码,163邮箱不需要
  56. MailSSLSocketFactory sf = new MailSSLSocketFactory();
  57. sf.setTrustAllHosts(true);
  58. properties.put("mail.smtp.ssl.enable", "true");
  59. properties.put("mail.smtp.ssl.socketFactory", sf);
  60. // 1.获取默认session对象
  61. Session session = Session.getDefaultInstance(properties, new Authenticator() {
  62. @Override
  63. public PasswordAuthentication getPasswordAuthentication() {
  64. return new PasswordAuthentication(FROM, VCode); // 发件人邮箱账号、授权码
  65. }
  66. });
  67. // 2.创建邮件对象
  68. Message message = new MimeMessage(session);
  69. // 2.1设置发件人
  70. message.setFrom(new InternetAddress(FROM));
  71. // 2.2设置接收人
  72. message.addRecipient(Message.RecipientType.TO, new InternetAddress(email));
  73. // 2.3设置邮件主题
  74. message.setSubject(title);
  75. // 2.4设置邮件内容
  76. String content = "<html><head></head><body><h1>这是一封"+title+"邮件</h1><h3>动态验证码:"
  77. + code + " </h3>请在5分钟内完成验证。如非本人操作请忽略。</body></html>";
  78. message.setContent(content, "text/html;charset=UTF-8");
  79. // 3.发送邮件
  80. Transport.send(message);
  81. System.out.println("邮件成功发送!");
  82. } catch (Exception e) {
  83. e.printStackTrace();
  84. }
  85. }
  86. }

//方法二:使用账号 密码

  1. public static void sendMessage(String email,String code,String title){
  2. try {
  3. Properties prop = new Properties();
  4. prop.setProperty("mail.host", "邮件服务器");
  5. prop.setProperty("mail.transport.protocol", "smtp");
  6. prop.setProperty("mail.smtp.auth", "true");
  7. prop.setProperty("mail.smtp.ssl.enable","true");
  8. //创建session
  9. Session session = Session.getInstance(prop);
  10. //开启Session的debug模式,这样就可以查看到程序发送Email的运行状态
  11. session.setDebug(true);
  12. //通过session得到transport对象
  13. Transport ts = session.getTransport();
  14. //使用邮箱的用户名和密码连上邮件服务器,发送邮件时,发件人需要提交邮箱的用户名和密码给smtp服务器,用户名和密码都通过验证之后才能够正常发送邮件给收件人。
  15. ts.connect("邮件服务器", "用户名", VCode);
  16. //创建邮件对象
  17. MimeMessage message = new MimeMessage(session);
  18. //指明邮件的发件人
  19. message.setFrom(new InternetAddress(FROM));
  20. //指明邮件的收件人
  21. message.setRecipient(Message.RecipientType.TO, new InternetAddress(email));
  22. //邮件的标题
  23. message.setSubject(title);
  24. //邮件的文本内容
  25. String content = "<html><head></head><body><h1> This is a"+title+".</h1><h3>Dynamic verification code:"
  26. + code + " .</h3>please complete the verification within 5 minutes. Please ignore if you are not operating by yourself.</body></html>";
  27. message.setContent(content, "text/html;charset=UTF-8");
  28. //发送邮件
  29. ts.sendMessage(message, message.getAllRecipients());
  30. ts.close();
  31. System.out.println("邮件成功发送!");
  32. } catch (Exception e) {
  33. e.printStackTrace();
  34. }
  35. }

发表评论

表情:
评论列表 (有 0 条评论,460人围观)

还没有评论,来说两句吧...

相关阅读