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"/>
    </form>

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">
    </form>

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;

import java.io.IOException;

public class SendEmailAction{
    private String emailAddress;

    public String execute() throws IOException {
        Email email = new Email();
        email.setSendTo(emailAddress);
        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)) {
            ServletActionContext.getRequest().getSession().setAttribute("code",code);
        }
        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>
        </action>

SendEmailService:

package com.hj.service;

import com.hj.beans.Email;

import java.io.IOException;
import java.io.InputStream;
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("prop_163.properties");
        Properties getEmailSender = new Properties();
        getEmailSender.load(ins);
        // 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.
        email.setSender(getEmailSender.getProperty("sender"));
        email.setPassword(getEmailSender.getProperty("password"));
        email.setMailDebug(getEmailSender.getProperty("mail_debug"));
        email.setMailSmtpAuth(getEmailSender.getProperty("mail_smtp_auth"));
        email.setMailHost(getEmailSender.getProperty("mail_host"));
        email.setMailTransportProtocol(getEmailSender.getProperty("mail_transport_protocol"));

        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("mail.host", 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 {
            msg.setSubject(email.getSubject());
            String code = email.getContent();
            //Set up text
            msg.setText(code);
            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) });
            transport.close();
        } 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:

qq:

163:

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)) {
            System.out.println("Success");
            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>
        </action>

Verify successful, that is, jump home page.

Required Jar packages, maven dependencies:

        <!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-core -->
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>2.5.20</version>
        </dependency>
        <!-- servlet Dependent jar package -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>
        <!-- jsp rely on jar package -->
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.1</version>
        </dependency>
        <!--jstl Label-dependent jar package -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <!-- Java Send mail -->
        <!-- https://mvnrepository.com/artifact/javax.mail/mail -->
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4.7</version>
        </dependency>

Tags: PHP Session Java JSP Apache

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