java implementation of user registration mailbox activation verification

Function: register the account through the email, and send the activation email to the email if the registration is successful. Prompt the user to log in the email for account activation before using the account.

Process: essentially, add a piece of data to the user table, and there should be a code field in the user table to store the random string. Code is randomly generated when adding users (uuid). When sending email, code value is brought to the email link to find a unique account, then judge the user status and activate.

The specific implementation is as follows:

1, Add maven required to send mail. javax.mail is used here
2, mail configuration in application

Here is the qq email used for testing. For how to obtain the authorization code, please click here: Get authorization code

3, MailConfig class corresponding load configuration
package com.weavewan.sdwan.user.config;

import lombok.Data;
import org.springframework.stereotype.Component;

 * @author wshanshi
 * @version 1.0
 * @date 2020/2/12 10:32
@ConfigurationProperties(prefix = "mail.config")
public class MailConfig {

    // Specify the host to send mail to
    private String host;

    // Mail server
    private String mailService;

    // Authentication
    private String auth;

    // Sender's email account
    private String sender;

    // Authorization code
    private String code;

    private String sslEnable;

    private String sslSocketFactory;

4, Mail tool class
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;

 * Mail sending class
 * @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;// Recipient mailbox
    private String code;// Activation code

    public MailUtil(String email, String code) { = email;
        this.code = code;

    public void run(String subject, String content) {

        // Create connection object javax.mail.Session
        // Create mail object javax.mail.Message
        // Send an activation email

        String sender = mailConfig.getSender();// Sender email
        String host = mailConfig.getHost(); // Specify the host of sending mail, (QQ) 𞓜 (Netease)

        Properties properties = System.getProperties();// Get system properties

        properties.setProperty(mailConfig.getMailService(), host);// Set up mail server
        properties.setProperty(mailConfig.getAuth(), "true");// Open authentication

        try {
            // QQ mailbox needs the following code, 163 mailbox does not need
            MailSSLSocketFactory sf = new MailSSLSocketFactory();
            properties.put(mailConfig.getSslEnable(), "true");
            properties.put(mailConfig.getSslSocketFactory(), sf);

            // Get the default session object
            Session session = Session.getDefaultInstance(properties, new Authenticator() {
                public PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(sender, mailConfig.getCode()); // Sender's email account and authorization code

            // Create mail object
            Message message = new MimeMessage(session);

            // Set sender
            message.setFrom(new InternetAddress(sender));

            // Set receiver
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(email));

            // Set message subject

            // Set message content
            message.setContent(content, "text/html;charset=UTF-8");

            // Send mail
            System.out.println("Message sent successfully!");
        } catch (Exception e) {
5, Call send mail

MailUtil mailUtil=new MailUtil(email.getS(),code.getS());
In mailUtil construction, two parameters are: the recipient mailbox (user mailbox) and the random code verification code (uuid).

The renderings are as follows:

Appendix: uuid generation

UUID uuid = UUID.randomUUID();
String str = uuid.toString();
String uuidStr = str.replace("-", "");
73 original articles published, praised 17, visited 40000+
Private letter follow

Tags: Session Maven Lombok Java

Posted on Thu, 13 Feb 2020 09:06:53 -0800 by Lenbot