註冊時,給使用者郵箱傳送啟用連結(java 傳送郵件)

star77266989發表於2015-08-26

(轉)http://www.cnblogs.com/xdp-gacl/p/4220190.html

註冊介面:

<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML>
<html>
  <head>
    <title>註冊頁面</title>
  </head>
  
  <body>
    <form action="${pageContext.request.contextPath}/servlet/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>

 傳送成功提示介面:

message.jsp

<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML>
<html>
  <head>
    <title>訊息提示頁面</title>
  </head>
  
  <body>
       ${message}
  </body>
</html>

 User.java

package com.hmx.controller;

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;
    }
}

 RegisterServlet.java

package com.hmx.controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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.java

package com.hmx.controller;

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;

/**
* @ClassName: Sendmail
* @Description: Sendmail類繼承Thread,因此Sendmail就是一個執行緒類,這個執行緒類用於給指定的使用者傳送Email
**/
public class SendMail extends Thread {
        //用於給使用者傳送郵件的郵箱
    private String from = "郵箱名稱";
        //郵箱的使用者名稱
    private String username = "郵箱名稱";
        //郵箱的密碼
    private String password = "密碼";
        //傳送郵件的伺服器地址(根據傳送郵箱情況修改)
    private String host = "smtp.qq.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: 建立要傳送的郵件
    *
    * @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() + ",請妥善保管,如有問題請聯絡網站客服!! \n 啟用連結";
        message.setContent(info, "text/html;charset=UTF-8");
        message.saveChanges();
        return message;
    }
}

 

注:

在QQ郵箱中有一項如果不設定,得到的會是 ”454 Authentication failed, please open smtp flag first! “

在QQ郵箱的設定裡面,找到賬戶-》POP3/IMAP/SMTP選擇開啟POP3/SMTP服務

相關文章