【Java】web项目中发送邮件验证码-Struts2
输入邮箱,获取验证码的页面:
<form action="email.action" method="post"> <input type="email" name="emailAddress"><br> <!-- 在Struts2中,这里的name属性中要和后台Action中的一致 --> <input type="submit" value="获取验证码"/> </form>
验证验证码的页面:
<form action="judge.action" method="post"> <input type="text" name="code"><br> <!-- 此处同理 --> <input type="submit" value="验证验证码"> </form>
这里没有使用Ajax生成验证码。
处理输入的邮箱,并调用发邮件的Service的Action类:
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("这是网页测试邮件-验证码"); int code = this.getCode(); email.setContent("验证码:"+code); System.out.println("验证码:"+code); SendEmailService sendEmailService = new SendEmailService(); String sendStatus = sendEmailService.sendEmail(email); if ("sendSuccess".equals(sendStatus)) { ServletActionContext.getRequest().getSession().setAttribute("code",code); } System.out.println("发送验证码成功"); 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的XML配置信息:
<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 { // 获取配置信息 InputStream ins = SendEmailService.class.getClassLoader().getResourceAsStream("prop_163.properties"); Properties getEmailSender = new Properties(); getEmailSender.load(ins); // 将配置信息保存到bean中,此处每次请求都需要获取配置信息,有些浪费资源,可以改造为只在服务器启动时获取一次 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(); // 收件人电子邮箱 String to = email.getSendTo(); // 发件人电子邮箱 String from = email.getSender(); // 开启debug调试 props.setProperty("mail.debug", email.getMailDebug()); // 发送服务器需要身份验证 props.setProperty("mail.smtp.auth", email.getMailSmtpAuth()); // 设置邮件服务器主机名 props.setProperty("mail.host", email.getMailHost()); // 发送邮件协议名称 props.setProperty("mail.transport.protocol", email.getMailTransportProtocol()); Session session = Session.getInstance(props); Message msg = new MimeMessage(session); //设置主题 try { msg.setSubject(email.getSubject()); String code = email.getContent(); //设置正文 msg.setText(code); msg.setFrom(new InternetAddress(from)); Transport transport = session.getTransport(); //登录邮箱,需要密码 transport.connect(email.getMailHost(), from, email.getPassword()); transport.sendMessage(msg, new Address[] { new InternetAddress(to) }); transport.close(); } catch (MessagingException e) { return "sendFail"; } return "sendSuccess"; } }
此处从配置文件中获取发邮件的配置信息为方便切换发邮件的邮箱信息,比如此处的163和qq邮箱的配置信息:
qq:
163:
此处就发送完成。并把验证码存在Session中。
验证只需将输入的验证码和Session中的验证码对比即可。
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("输入的验证码"+sendCode+" 对比中。。。"); if (code.equals(sendCode)) { System.out.println("成功"); return "success"; } return "fail"; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } }
JudgeCodeAction的Struts2的XML配置文件
<action name="judge" class="com.hj.action.JudgeCode"> <result name="success">index.jsp</result> </action>
验证成功,即跳转主页。
所需要的Jar包,maven依赖:
<!-- 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依赖的jar包 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <!-- jsp依赖jar包 --> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>2.3.1</version> </dependency> <!--jstl标签依赖的jar包 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- Java 发送邮件 --> <!-- https://mvnrepository.com/artifact/javax.mail/mail --> <dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.7</version> </dependency>