Function: registered successfully sends activation email to the mailbox by registered mail account. It prompts the user to log mailbox accounts activated in order to use the account.
Process: essentially new user data to a table, user table stored in the code field should have a random string. adding random code generated when a user (UUID), sending mail messages linked to the value of the code used to find the unique account, then the user is determined state, activation.
Specific achieve the following:
First, add maven send a message needed here is 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>
Two, application of the configuration mail
Here the test used qq-mail on how to obtain a license key, please click here: to obtain an authorization code
Three, MailConfig loading configuration corresponding to the class
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;
}
Four, Mail Tools
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();
}
}
}
Fifth, the call to send a message
= New new MailUtil mailUtil MailUtil (email.getS (), code.getS ());
mailUtil configuration parameters are two: the recipient's mailbox (mailbox user), a random code authentication code (uuid).
The effect is as follows:
Annex: generating a UUID
UUID uuid = UUID.randomUUID();
String str = uuid.toString();
String uuidStr = str.replace("-", "");