javaWeb邮箱验证
我们的日常生活中用到很多用户注册啦之类的,为了提高安全性和用户体验,我们也会遇到邮箱通过邮箱确定验证信息的例子。
首先我们看一下需要哪些jar包,其中activation.jar和mail.jar是邮件发送不可少的,servlet和jstl在程序中也有用到,所以大家先将jar包导入项目中区。
然后就是我们查看邮箱smtp的服务器地址
RegisterServlet
package com.yuehailin.controller; /** * Created by yuehailin on 2017/12/27. */ import com.yuehailin.domain.User; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class RegisterServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try{ String username = request.getParameter("username"); String password = request.getParameter("password"); String email = request.getParameter("email"); User user = new User(); user.setEmail(email); user.setPassword(password); user.setUsername(username); System.out.println("把用户信息注册到数据库中"); //用户注册成功之后就使用用户注册时的邮箱给用户发送一封Email //发送邮件是一件非常耗时的事情,因此这里开辟了另一个线程来专门发送邮件 Sendmail send = new Sendmail(user); //启动线程,线程启动之后就会执行run方法来发送邮件 send.start(); //注册用户 //new UserService().registerUser(user); request.setAttribute("message", "恭喜您,注册成功,我们已经发了一封带了注册信息的电子邮件,请查收,如果没有收到,可能是网络原因,过一会儿就收到了!!"); request.getRequestDispatcher("/message.jsp").forward(request, response); }catch (Exception e) { e.printStackTrace(); request.setAttribute("message", "注册失败!!"); request.getRequestDispatcher("/message.jsp").forward(request, response); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }类
Sendmail
package com.yuehailin.controller; /** * Created by yuehailin on 2017/12/27. */ import com.yuehailin.domain.User; import javax.mail.Message; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import java.util.Properties; public class Sendmail extends Thread { //用于给用户发送邮件的邮箱 // PS: 某些邮箱服务器为了增加邮箱本身密码的安全性,给 SMTP 客户端设置了独立密码(有的邮箱称为“授权码”), // 对于开启了独立密码的邮箱, 这里的邮箱密码必需使用这个独立密码(授权码)。 private String from = "yuehailin99@163.com"; //邮箱的用户名 private String username = "yuehailin99"; //邮箱的密码 // 对于开启了独立密码的邮箱, 这里的邮箱密码必需使用这个独立密码(授权码)。// 发件人邮箱的 SMTP 服务器地址, 必须准确,不同邮件服务器地址不同, 一般(只是一般,绝非绝对)格式为: smtp.xxx.com // 网易163邮箱的 SMTP 服务器地址为: smtp.163.com private String password = "yuehailin99"; //发送邮件的服务器地址 private String host = "smtp.163.com"; private User user; public Sendmail(User user){ this.user = user; } /* 重写run方法的实现,在run方法中发送邮件给指定的用户 * @see java.lang.Thread#run() */ @Override public void run() { try{ Properties prop = new Properties(); prop.setProperty("mail.host", host); prop.setProperty("mail.transport.protocol", "smtp"); prop.setProperty("mail.smtp.auth", "true"); Session session = Session.getInstance(prop); session.setDebug(true); Transport ts = session.getTransport(); ts.connect(host, username, password); Message message = createEmail(session,user); ts.sendMessage(message, message.getAllRecipients()); ts.close(); }catch (Exception e) { throw new RuntimeException(e); } } /** * @Method: createEmail * @Description: 创建要发送的邮件 * @Anthor:孤傲苍狼 * * @param session * @param user * @return * @throws Exception */ public Message createEmail(Session session, User user) throws Exception{ MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); message.setRecipient(Message.RecipientType.TO, new InternetAddress(user.getEmail())); message.setSubject("用户注册邮件"); String info = "恭喜您注册成功,您的用户名:" + user.getUsername() + ",您的密码:" + user.getPassword() + ",请妥善保管,如有问题请联系网站客服!!"; message.setContent(info, "text/html;charset=UTF-8"); message.saveChanges(); return message; } }
package com.yuehailin.domain; /** * Created by yuehailin on 2017/12/27. */ public class User { private String username; private String password; private String email; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }
<%@ page language="java" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE HTML> <html> <head> <title>注册页面</title> </head> <body> <form action="<c:url value='/RegisterServlet'/>" method="post"> 用户名:<input type="text" name="username"><br/> 密码:<input type="password" name="password"><br/> 邮箱:<input type="text" name="email"><br/> <input type="submit" value="注册"> </form> </body> </html>
<%@ page language="java" pageEncoding="UTF-8"%> <!DOCTYPE HTML> <html> <head> <title>消息提示页面</title> </head> <body> ${message} </body> </html>
本文链接:https://www.kkkliao.cn/?id=29 转载需授权!
版权声明:本文由廖万里的博客发布,如需转载请注明出处。