Send Mail Verification Code-Struts2 in web Project

Enter the mailbox to get the page of authentication code:

    <form action="email.action" method="post">
        <input type="email" name="emailAddress"><br> <!-- stay Struts2 Chinese, here's the name Attributes in and behind the scenes Action Consistency in -->
        <input type="submit" value="Get the authentication code"/>

Verification code page:

    <form action="judge.action" method="post">
        <input type="text" name="code"><br>  <!-- Here is the same reason. -->
        <input type="submit" value="Verification Verification Code">

Ajax is not used here to generate validation codes.

Processing the incoming mailbox and calling the Action class of the Service that sent the mail:

package com.hj.action;

import com.hj.beans.Email;
import com.hj.service.SendEmailService;
import org.apache.struts2.ServletActionContext;


public class SendEmailAction{
    private String emailAddress;

    public String execute() throws IOException {
        Email email = new Email();
        email.setSubject("This is the web test email.-Verification Code");
        int code = this.getCode();
        email.setContent("Verification Code:"+code);
        System.out.println("Verification Code:"+code);
        SendEmailService sendEmailService = new SendEmailService();
        String sendStatus = sendEmailService.sendEmail(email);
        if ("sendSuccess".equals(sendStatus)) {
        System.out.println("Successful sending of authentication code");
        return "success";

    private int getCode(){
        int code = (int) (Math.random()*8999+1000);
        return code;

    public String getEmailAddress() {
        return emailAddress;

    public void setEmailAddress(String emailAddress) {
        this.emailAddress = emailAddress;

sendEmailAction's XML configuration information:

        <action name="email" class="com.hj.action.SendEmailAction">
            <result name="success">sendCode.jsp</result>


package com.hj.service;

import com.hj.beans.Email;

import java.util.Properties;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendEmailService {

    public String sendEmail(Email email) throws IOException {
        // Get configuration information
        InputStream ins = SendEmailService.class.getClassLoader().getResourceAsStream("");
        Properties getEmailSender = new Properties();
        // Save configuration information to bean In this case, each request requires configuration information, which is a waste of resources and can be modified to retrieve only once at server startup.

        Properties props = new Properties();
        // Recipient's E-mail
        String to = email.getSendTo();
        // Sender's E-mail
        String from = email.getSender();
        // open debug debugging
        props.setProperty("mail.debug", email.getMailDebug());
        // The sending server needs authentication
        props.setProperty("mail.smtp.auth", email.getMailSmtpAuth());
        // Setting Mail Server Host Name
        props.setProperty("", email.getMailHost());
        // Send mail protocol name
        props.setProperty("mail.transport.protocol", email.getMailTransportProtocol());

        Session session = Session.getInstance(props);

        Message msg = new MimeMessage(session);
        //set up themes
        try {
            String code = email.getContent();
            //Set up text
            msg.setFrom(new InternetAddress(from));
            Transport transport = session.getTransport();
            //Login to Mailbox, Password Required
            transport.connect(email.getMailHost(), from, email.getPassword());
            transport.sendMessage(msg, new Address[] { new InternetAddress(to) });
        } catch (MessagingException e) {
            return "sendFail";
        return "sendSuccess";

Here, the configuration information of email is obtained from the configuration file for the convenience of switching the mailbox information of email, such as the configuration information of 163 and qq mailboxes here:



Send it here. The verification code is stored in Session.

The validation only needs to compare the input validation code with the validation code in Session.

Code for JudgeCodeAction:

package com.hj.action;

import org.apache.struts2.ServletActionContext;

public class JudgeCode {
    private String code;

    public String execute(){
        String sendCode = String.valueOf(ServletActionContext.getRequest().getSession().getAttribute("code"));
        System.out.println("Input Verification Code"+sendCode+" In contrast...");
        if (code.equals(sendCode)) {
            return "success";
        return "fail";

    public String getCode() {
        return code;

    public void setCode(String code) {
        this.code = code;

XML configuration file for Struts2 of JudgeCodeAction

        <action name="judge" class="com.hj.action.JudgeCode">
            <result name="success">index.jsp</result>

Verify successful, that is, jump home page.

Required Jar packages, maven dependencies:

        <!-- -->
        <!-- servlet Dependent jar package -->
        <!-- jsp rely on jar package -->
        <!--jstl Label-dependent jar package -->
        <!-- Java Send mail -->
        <!-- -->

Tags: PHP Session Java JSP Apache

Posted on Mon, 07 Oct 2019 15:32:05 -0700 by shinephp