轉自:http://www.cnblogs.com/xdp-gacl/p/4220190.htmlhtml
如今不少的網站都提供有用戶註冊功能, 一般咱們註冊成功以後就會收到一封來自注冊網站的郵件。郵件裏面的內容可能包含了咱們的註冊的用戶名和密碼以及一個激活帳戶的超連接等信息。今天咱們也來實現一個這樣的功能,用戶註冊成功以後,就將用戶的註冊信息以Email的形式發送到用戶的註冊郵箱當中,實現發送郵件功能就得藉助於JavaMail了。java
register.jspweb
1 <%@ page language="java" pageEncoding="UTF-8"%> 2 <!DOCTYPE HTML> 3 <html> 4 <head> 5 <title>註冊頁面</title> 6 </head> 7 8 <body> 9 <form action="${pageContext.request.contextPath}/servlet/RegisterServlet" method="post"> 10 用戶名:<input type="text" name="username"><br/> 11 密碼:<input type="password" name="password"><br/> 12 郵箱:<input type="text" name="email"><br/> 13 <input type="submit" value="註冊"> 14 </form> 15 </body> 16 </html>
message.jsp數據庫
1 <%@ page language="java" pageEncoding="UTF-8"%> 2 <!DOCTYPE HTML> 3 <html> 4 <head> 5 <title>消息提示頁面</title> 6 </head> 7 8 <body> 9 ${message} 10 </body> 11 </html>
User.java服務器
1 package me.gacl.domain; 2 3 public class User { 4 private String username; 5 private String password; 6 private String email; 7 public String getUsername() { 8 return username; 9 } 10 public void setUsername(String username) { 11 this.username = username; 12 } 13 public String getPassword() { 14 return password; 15 } 16 public void setPassword(String password) { 17 this.password = password; 18 } 19 public String getEmail() { 20 return email; 21 } 22 public void setEmail(String email) { 23 this.email = email; 24 } 25 }
發送郵件是一件很是耗時的事情,所以這裏設計一個線程類來發送郵件網絡
1 package me.gacl.web.controller; 2 3 import java.util.Properties; 4 import javax.mail.Message; 5 import javax.mail.Session; 6 import javax.mail.Transport; 7 import javax.mail.internet.InternetAddress; 8 import javax.mail.internet.MimeMessage; 9 import me.gacl.domain.User; 10 11 /** 12 * @ClassName: Sendmail 13 * @Description: Sendmail類繼承Thread,所以Sendmail就是一個線程類,這個線程類用於給指定的用戶發送Email 14 * @author: 孤傲蒼狼 15 * @date: 2015-1-12 下午10:43:48 16 * 17 */ 18 public class Sendmail extends Thread { 19 //用於給用戶發送郵件的郵箱 20 private String from = "gacl@sohu.com"; 21 //郵箱的用戶名 22 private String username = "gacl"; 23 //郵箱的密碼 24 private String password = "郵箱密碼"; 25 //發送郵件的服務器地址 26 private String host = "smtp.sohu.com"; 27 28 private User user; 29 public Sendmail(User user){ 30 this.user = user; 31 } 32 33 /* 重寫run方法的實現,在run方法中發送郵件給指定的用戶 34 * @see java.lang.Thread#run() 35 */ 36 @Override 37 public void run() { 38 try{ 39 Properties prop = new Properties(); 40 prop.setProperty("mail.host", host); 41 prop.setProperty("mail.transport.protocol", "smtp"); 42 prop.setProperty("mail.smtp.auth", "true"); 43 Session session = Session.getInstance(prop); 44 session.setDebug(true); 45 Transport ts = session.getTransport(); 46 ts.connect(host, username, password); 47 Message message = createEmail(session,user); 48 ts.sendMessage(message, message.getAllRecipients()); 49 ts.close(); 50 }catch (Exception e) { 51 throw new RuntimeException(e); 52 } 53 } 54 55 /** 56 * @Method: createEmail 57 * @Description: 建立要發送的郵件 58 * @Anthor:孤傲蒼狼 59 * 60 * @param session 61 * @param user 62 * @return 63 * @throws Exception 64 */ 65 public Message createEmail(Session session,User user) throws Exception{ 66 67 MimeMessage message = new MimeMessage(session); 68 message.setFrom(new InternetAddress(from)); 69 message.setRecipient(Message.RecipientType.TO, new InternetAddress(user.getEmail())); 70 message.setSubject("用戶註冊郵件"); 71 72 String info = "恭喜您註冊成功,您的用戶名:" + user.getUsername() + ",您的密碼:" + user.getPassword() + ",請妥善保管,若有問題請聯繫網站客服!!"; 73 message.setContent(info, "text/html;charset=UTF-8"); 74 message.saveChanges(); 75 return message; 76 } 77 }
1 package me.gacl.web.controller; 2 3 import java.io.IOException; 4 import javax.servlet.ServletException; 5 import javax.servlet.http.HttpServlet; 6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpServletResponse; 8 import me.gacl.domain.User; 9 import me.gacl.service.UserService; 10 11 public class RegisterServlet extends HttpServlet { 12 13 public void doGet(HttpServletRequest request, HttpServletResponse response) 14 throws ServletException, IOException { 15 try{ 16 String username = request.getParameter("username"); 17 String password = request.getParameter("password"); 18 String email = request.getParameter("email"); 19 User user = new User(); 20 user.setEmail(email); 21 user.setPassword(password); 22 user.setUsername(username); 23 24 System.out.println("把用戶信息註冊到數據庫中"); 25 //用戶註冊成功以後就使用用戶註冊時的郵箱給用戶發送一封Email 26 //發送郵件是一件很是耗時的事情,所以這裏開闢了另外一個線程來專門發送郵件 27 Sendmail send = new Sendmail(user); 28 //啓動線程,線程啓動以後就會執行run方法來發送郵件 29 send.start(); 30 31 //註冊用戶 32 //new UserService().registerUser(user); 33 request.setAttribute("message", "恭喜您,註冊成功,咱們已經發了一封帶了註冊信息的電子郵件,請查收,若是沒有收到,多是網絡緣由,過一下子就收到了!!"); 34 request.getRequestDispatcher("/message.jsp").forward(request, response); 35 }catch (Exception e) { 36 e.printStackTrace(); 37 request.setAttribute("message", "註冊失敗!!"); 38 request.getRequestDispatcher("/message.jsp").forward(request, response); 39 } 40 } 41 42 public void doPost(HttpServletRequest request, HttpServletResponse response) 43 throws ServletException, IOException { 44 doGet(request, response); 45 } 46 }
程序運行效果以下:session
如今不少網站都有這樣的功能,用戶註冊完成以後,網站根據咱們註冊時填寫的郵箱給咱們一封Email,而後點擊Email中的超連接去激活咱們的用戶。這種功能就是這樣實現的。dom
在總結使用JavaMail發送郵件時發現,將郵件發送到sina或者sohu的郵箱時,不必定可以立刻收取獲得郵件,老是有延遲,有時甚至會延遲很長的時間,甚至會被當成垃圾郵件來處理掉,或者乾脆就拒絕接收,有時候爲了看到郵件發送成功的效果,要等半天,實屬無奈啊。jsp