首先在tomcat安装目录下的lib目录下导入两个jar包:activation.jar 和mail.jar
简陋的注册页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>注册页面</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form action="${pageContext.request.contextPath }/servlet/RegisterServlet"> 用户名:<input type="text" name="username"><br/> 密码:<input type="password" name="password"><br/> 邮箱:<input type="text" name="mail"><br/> <input type="submit" value="提交"> </form> </body> </html>处理注册功能的servlet
package cn.lfd.web.controller; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import cn.lfd.domain.User; import cn.lfd.utils.SendMail; 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 mail = request.getParameter("mail"); User user = new User(); user.setPasswrod(password); user.setUsername(username); user.setMail(mail); SendMail send = new SendMail(user);//起一个线程发送邮件 send.start(); System.out.println("把用户信息注册到数据库中"); 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); } }发送邮件的线程:
package cn.lfd.utils; import java.util.Properties; import javax.mail.Message; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import javax.naming.Context; import javax.naming.InitialContext; import cn.lfd.domain.User; /* * 发送邮件的线程 */ public class SendMail extends Thread { private User user; private static String from = "[email protected]";//邮件发送账户 private static String host = "smtp.163.com";//邮件服务器地址 private static String username = "18770913843";//用户名 private static String password = "lfdlovehtzyy000";//密码 public SendMail(User user) { this.user = user; } @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");*/ //通过jndi创建session Context initCtx = new InitialContext(); Context envCtx = (Context) initCtx.lookup("java:comp/env"); Session session = (Session) envCtx.lookup("mail/Session"); //Session session = Session.getInstance(prop); Message message = makeMessage(session, user); Transport ts = session.getTransport(); ts.connect(host, username, password);//与邮件服务器建立连接 ts.sendMessage(message, message.getAllRecipients()); }catch(Exception e) { e.printStackTrace(); throw new RuntimeException(e); } } //创建一封注册邮件 private Message makeMessage(Session session, User user) throws Exception { MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); message.setRecipient(Message.RecipientType.TO, new InternetAddress(user.getMail())); message.setSubject("注册邮件"); String info = "恭喜你注册成功,你的用户名是"+user.getUsername()+", 密码是"+user.getPasswrod()+",请妥善保管你的用户名和密码!!!"; message.setContent(info, "text/html;charset=UTF-8"); message.saveChanges(); return message; } }用户实体类:User
package cn.lfd.domain; public class User { private String username; private String passwrod; private String mail; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPasswrod() { return passwrod; } public void setPasswrod(String passwrod) { this.passwrod = passwrod; } public String getMail() { return mail; } public void setMail(String mail) { this.mail = mail; } }最后在Web-Root下面的META-INF下面创建一个context.xml文件
<Context> <Resource name="mail/Session" auth="Container" type="javax.mail.Session" mail.smtp.host="smtp.163.com" mail.transport.protocol="smtp" mail.smtp.auth="true"/> </Context>最后进入工程的lib目录下把 activation.jar 和mail.jar删了