java实现用户注册邮箱激活验证 您所在的位置:网站首页 java实现邮箱登录 java实现用户注册邮箱激活验证

java实现用户注册邮箱激活验证

2023-09-13 00:05| 来源: 网络整理| 查看: 265

功能:通过邮箱注册账号,注册成功会向邮箱发送激活邮件。提示用户登录邮箱进行账户激活,方可使用账号。

流程:本质上就是向user表里新增一条数据,user表中应有一个code字段存放随机串。code在添加用户时随机生成(uuid),发送邮件时把code值带到邮件链接中用于查找唯一账户,然后判断用户状态,进行激活。

具体实现如下:

一、添加发送邮件需要的maven,这里用的是javax.mail javax.mail mail 1.4.7 javax.activation activation 1.1.1 二、application中mail配置

在这里插入图片描述 这里测试使用的qq邮箱,关于如何获取授权码请点击这里:获取授权码

三、MailConfig类对应加载配置 package com.weavewan.sdwan.user.config; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; /** * @author wshanshi * @version 1.0 * @date 2020/2/12 10:32 */ @Data @Component @ConfigurationProperties(prefix = "mail.config") public class MailConfig { // 指定发送邮件的主机 private String host; // 邮件服务器 private String mailService; // 认证 private String auth; // 发件人邮箱账号 private String sender; // 授权码 private String code; private String sslEnable; private String sslSocketFactory; } 四、Mail工具类 package com.weavewan.sdwan.user.util; import com.sun.mail.util.MailSSLSocketFactory; import com.weavewan.sdwan.user.config.MailConfig; import org.springframework.context.ApplicationContext; import javax.mail.*; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import java.util.Properties; /** * 邮件发送类 * * @author wshanshi * @version 1.0 * @date 2020/2/8 13:57 */ public class MailUtil { private ApplicationContext applicationContext = SpringUtils.getApplicationContext(); private MailConfig mailConfig = applicationContext.getBean(MailConfig.class); private String email;// 收件人邮箱 private String code;// 激活码 public MailUtil(String email, String code) { this.email = email; this.code = code; } public void run(String subject, String content) { // 创建连接对象javax.mail.Session // 创建邮件对象 javax.mail.Message // 发送一封激活邮件 String sender = mailConfig.getSender();// 发件人电子邮箱 String host = mailConfig.getHost(); // 指定发送邮件的主机smtp.qq.com(QQ)|smtp.163.com(网易) Properties properties = System.getProperties();// 获取系统属性 properties.setProperty(mailConfig.getMailService(), host);// 设置邮件服务器 properties.setProperty(mailConfig.getAuth(), "true");// 打开认证 try { // QQ邮箱需要下面这段代码,163邮箱不需要 MailSSLSocketFactory sf = new MailSSLSocketFactory(); sf.setTrustAllHosts(true); properties.put(mailConfig.getSslEnable(), "true"); properties.put(mailConfig.getSslSocketFactory(), sf); // 获取默认session对象 Session session = Session.getDefaultInstance(properties, new Authenticator() { public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(sender, mailConfig.getCode()); // 发件人邮箱账号、授权码 } }); // 创建邮件对象 Message message = new MimeMessage(session); // 设置发件人 message.setFrom(new InternetAddress(sender)); // 设置接收人 message.addRecipient(Message.RecipientType.TO, new InternetAddress(email)); // 设置邮件主题 message.setSubject(subject); // 设置邮件内容 message.setContent(content, "text/html;charset=UTF-8"); // 发送邮件 Transport.send(message); System.out.println("邮件成功发送!"); } catch (Exception e) { e.printStackTrace(); } } } 五、调用发送邮件

MailUtil mailUtil=new MailUtil(email.getS(),code.getS()); mailUtil构造中两个参数分别为:收件人邮箱(用户邮箱)、随机code验证码(uuid)。 在这里插入图片描述 效果图如下: 在这里插入图片描述 温馨提示:成功了别忘记给楼主小姐姐点个 赞 哇哈哈哈哈哈哈。

附:uuid生成

UUID uuid = UUID.randomUUID(); String str = uuid.toString(); String uuidStr = str.replace("-", "");


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有