Javamailsender implements mailbox authentication

This paper starts with the problems encountered in mailbox registration verification through javamailsender, and analyzes its principle and solutions in detail.

To use email registration verification, we need to clarify the design idea:

Question 1: after submitting the registration information, you need to send an email to the filled email number

Question 2: how to activate the user when the mail arrives, whether through the get request or the verification code (this article uses the get interface to activate)

Question 3: how to set the effective time for email activation

Through the above three questions, bloggers can help you master javamailsender mailbox verification

Question one

First, I need to solve how to send mail to the specified mailbox number

Add the following dependencies to POM:

Yes, we use the mail dependency under the javax expansion package, so don't choose the wrong import in the code

This code is simple for everyone to understand

From top to bottom, let's look at the interface first:

Because it is a test, bloggers create fake data of users, and getting email is the key

Creating a new thread to send mail is to improve the user experience

Imagine that if it is not asynchronous, users need to wait for the mail to be sent before jumping to the page

Let's look at the business layer method of mail sending:

This method explains:

1. If your server mailbox is QQ mailbox, it is modified to mailsender setHost("smtp.qq.com");

2. Mimemessagehelper is an extended class of mail under javax. If you use mail encapsulated by spring, you don't need it. If spring mail is used, the set method will be different

3.helper. setText(url,true); The second parameter true indicates that the current string is in HTML format, so the tags will work

4.getEmailToken(user); The method will be discussed below

So far, the mail can be sent normally. For my convenience, I give the HTML page code:

Question two

How to use the API of get request to activate mail?

In question 1, getemailtoken (user) under senemail () is used for activation

It can be seen that the email content sent by senemail () is a hyperlink, which is used to start our activation interface

But what does this have to do with getemailtoken (user)

From the literal meaning, we will use a token here

Let's start by activating the controller

Through the hyperlink, we send an emailtoken parameter to the server

After the server gets the token, it will compare it with the local token. If it is the same, it will pass the verification

Here, I use redis as the cache, with token as the key and user information as the value

Look at two methods:

The first method uses redis to convert the user information into a string and store it in memory

The second method is to activate verification, check whether there is a value in redis through key (token), and return true successfully

Question three

What about setting the validity period of mailbox verification?

At this point, students who often use redis may have guessed

Just add a line of code in getemailtoken():

Yes, use redistemplate Expire() sets the expiration time of the current key value pair

Finally, let's take a look at the activation email:

The above is all about the implementation of email registration and verification by javamailsender. If you still don't understand, you can discuss it in the message area below.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>