功能:通过邮箱注册账号,注册成功会向邮箱发送激活邮件。提示用户登录邮箱进行账户激活,方可使用账号。
流程:本质上就是向user表里新增一条数据,user表中应有一个code字段存放随机串。code在添加用户时随机生成(uuid),发送邮件时把code值带到邮件链接中用于查找唯一账户,然后判断用户状态,进行激活。
具体实现如下:
一、添加发送邮件需要的maven,这里用的是javax.mail
<dependency><groupId>javax.mail</groupId><artifactId>mail</artifactId><version>1.4.7</version></dependency><dependency><groupId>javax.activation</groupId><artifactId>activation</artifactId><version>1.1.1</version></dependency>
二、application中mail配置
这里测试使用的qq邮箱,关于如何获取授权码请点击这里:获取授权码
三、MailConfig类对应加载配置
package com.weavewan.sdwan.user.config;import lombok.Data;import org.springframework.boot.context.properties.ConfigurationProperties;import org.ponent;/*** @author wshanshi* @version 1.0* @date /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 /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(); // 指定发送邮件的主机(QQ)|(网易)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("-", "");