Today is the 104th day for Liu Xiaoai to learn Java by herself.

Thank you for watching. Thank you.

Without further ado, let’s begin today’s lesson:

To register a website, you usually need an email activation:

After registering, the site will send an activation message to the email address you fill in. Click the corresponding link to complete the activation.

After successful activation, you can log in to the website normally.

This can be done with Java code:

On email activation, I honestly wasn’t going to spend a whole night studying it.

After all, this type of knowledge needs relatively few, even if encountered through the network can also be solved.

Then I thought, I can access other people’s tutorials online, so why not have them access my notes?

So I still spent a night to learn, but also convenient for myself to meet the need to check later.

First, Java sending mailbox

Since it is a sending mailbox, we need a fixed sending mailbox address first, and this mailbox needs to open POP3 service.

I use the email provided by netease. The service can be enabled by setting the email.

After starting the service, you need to verify the mobile phone number; Successful authentication will result in an authorization code, which needs to be written down.

Once you have this authorization code, you can send the mailbox from Java code, which is the mailbox.

Of course, there are a lot of techniques for sending emails. I use Commons -mail provided by Apache.

After importing the dependency, you can write the code.

The code is written as follows:

① Create the htmlEamil object

The HtmlEmail class is used in apache-Mail to manipulate mail.

② Set server parameters

SetHostName: Sets the server address.

SetAuthentication: so you need to enter your email address and authorization code.

③ Set the sender and recipient

SetCharset: set the coding format for “GB2312”, a set of standards formulated by the country, GB is the abbreviation of the Pinyin of the NATIONAL standard.

SetFrom: This is the email address from which the authorization code was obtained.

SetaddTo: This is to set the recipient, that is, who registered is whose mailbox, dynamic access, MY side of the test will first use my own mailbox instead.

4. Set the subject and body of the email

SetSubject: The subject of an email sent to the user, usually “XXX website activation email”

SetMsg: Send the text, usually “Congratulations, registration success! Please click on the link to activate “, along with a link.

I’ll just fill in a random link here as a test.

⑤ Send an email

Send Sends an email.

Second, send email and test

1 Encapsulate configuration files

As with JDBC, you can wrap the above code into a utility class, and the configuration information about the server can be wrapped into a configuration file.

① Load the configuration file using the class loader

MailUtil. Class. GetClassLoader () : get MailUtil class loader.

GetResourceAsStream () : Translation is to fetch the source file as a stream with the configuration file path.

This gets the flow corresponding to the profile.

② Obtain each attribute in the configuration file

Load () : loads the corresponding stream into the properties.

GetProperty () : Gets the value of the parameter.

2 Mail encapsulation

Encapsulate the mailing code in a method and make it more extensible through configuration files.

Create a static method sendEamil() in our custom utility class MailUtil:

Encapsulate the code to send the mail into this method. When you need to send the mail, call the sendEamil method with MailUtil.

Where sendEamil method has two parameters:

  • UserEamil: specifies the email address entered during user registration, that is, the recipient’s email address.
  • MSG: Indicates the email message to be sent to the user.

3 Complete the email code

When do you send the email?

After the user is successfully activated, the email can be sent:

In the UserServlet, the registration function was implemented yesterday, and the registerFlag responds to the front end as a result.

If the value is true, it indicates that the registration is successful. As a judgment, you can insert a code to realize the function of sending emails.

Note the writing of links in MSG:

When the user clicks on the link after receiving the mailbox, a request is sent to the userServlet.

This is the request we need to accept, so the path is filled with userServlet and takes two parameters:

  • MethodName: active business, with a method corresponding to it in the UserServlet.
  • Code: indicates the current user’s code.

4 Registration page test

After completing the send mail code, do a test to determine whether the mail can be sent.

On the user registration page, after successful registration, the server mailbox I set will send an activation email to the mailbox filled by the user.

3. Mailbox activation service realization

When the user clicks the link in the activation email, the request is sent to the UserServlet, which is the path we filled in above.

1. Activate the Web layer of the business implementation

Set up a method in the UserServlet to accept the request, with the method name being the parameter active in the request.

① Get the data carried in the request

There is a parameter code that finds the corresponding user in the database and modifies its activation status.

② Activation succeeded

If activated successfully, you are redirected to the login page and respond to a prompt to alert the user before doing so.

Responsetheader (), which takes two parameters:

  • Refresh your memory.

  • 3; Login. HTML: 3 indicates that the page is refreshed every 3 seconds and separated by semicolons (;). Login. HTML indicates the page to be redirected.

③ Activation failed

The principle is the same as above, modify the prompt message to remind the user to register again, and skip to the registration page.

Activate the Service and DAO layers of the business implementation

There is not much business logic involved in activating the business itself, so there is not much code to write.

The operation on the database is a modification operation, so the update method of Template is used.

The above is the realization of mailbox activation service:

  • If the user is activated successfully, the login page is displayed.
  • User activation failure, will jump to the registration page, the test phase of the general activation failure may be written by their own code problems.

The last

Thanks for watching.

If you can, please give it a thumbs up. Thank you.