JavaWeb QQ郵箱找回密碼
阿新 • • 發佈:2018-12-02
我的上一篇部落格,已經寫了登入註冊,接下來寫QQ郵箱找回密碼
首先:我們需要在 QQ郵箱設定中獲取授權碼,步驟如下
進入QQ郵箱---->設定---->賬戶---->開啟服務:POP3/SMTP服務---->儲存授權碼
**其次:**我百度雲盤裡儲存了可以提取,也可以百度下載
連結:https://pan.baidu.com/s/1IT_1sDNvALVh-H_78nMXaA
提取碼:btnb
匯入專案中 或者
**最後:**程式碼展示
1.SendEmial.jsp(前端頁面)
<form name="f1" id="f1" action="sedEmail" method="post">
<table border="0">
<tr>
<td colspan="1">
<center>
<h3>郵箱找回密碼</h3>
</center>
</td>
</tr>
<tr>
<td>
<input type="text" name="email" id="email" placeholder= "請輸入您的郵箱號">
</td>
<td colspan="1">
<center>
<font color="red" size="2"> ${MSG}</font>
</center>
</td>
</tr>
<tr>
<td>
<input type="submit" value="確認">
</td>
<td colspan= "1">
<center>
<font color="red" size="2"> ${MSG3}</font>
</center>
</td>
</tr>
</table>
</form>
<a href="login.jsp" style="margin-left: 70px;"><font size="2"><i>返回登入</i>
</font> </a>
2.CheckSendEmail.java(servlet進行資料處理,傳送郵件)
package com.aiit.service;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.aiit.dao.SendEmail;
import com.aiit.model.Login;
@WebServlet("/sedEmail")
public class CheckSendEmail extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//接收頁面傳過來的QQ郵箱號
String email = request.getParameter("email");
//傳到資料庫,判斷是否存在,如果存在,返回使用者賬號資訊
SendEmail send = new SendEmail();
Login login = send.getEmail(email);
//System.out.println(login.getEmail());
//to接收的是使用者註冊時的郵箱號,也就是就是收件人的,將接收到後臺傳送的密碼
String to = login.getEmail();
// 發件人電子郵箱,你可以改成自己的郵箱號
String from = "********@qq.com";
// 指定傳送郵件的主機為 smtp.qq.com
String host = "smtp.qq.com"; //QQ 郵件伺服器
// 獲取系統屬性
Properties properties = System.getProperties();
// 設定郵件伺服器
properties.setProperty("mail.smtp.host", host);
properties.put("mail.smtp.auth", "true");
// 獲取預設session物件
Session session = Session.getDefaultInstance(properties,new Authenticator(){
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication("********@qq.com", "kwifhodgdpbldigd");
//發件人郵件使用者名稱、授權碼(授權碼要與QQ郵箱相對應,可以從郵箱設定裡面獲得,詳細步驟在部落格開頭)
}
});
try{
// 建立預設的 MimeMessage 物件
MimeMessage message = new MimeMessage(session);
// Set From: 頭部頭欄位
message.setFrom(new InternetAddress(from));
// Set To: 頭部頭欄位
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
// Set Subject: 頭部頭欄位
message.setSubject("This is the Subject Line!");
// 設定訊息體
message.setText("您的員工管理系統,密碼是:"+login.getLoginPwd());
// 傳送訊息
Transport.send(message);
//System.out.println("Sent message successfully....from runoob.com");
//傳到頁面
request.setAttribute("MSG3", "傳送成功,請注意查收!");
request.getRequestDispatcher("SendEmial.jsp").forward(request, response);
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
3.SendEmail.java(資料庫)
package com.aiit.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.aiit.common.JDBCUtils;
import com.aiit.model.Login;
public class SendEmail {
public Login getEmail(String email){
//先查詢前端頁面傳過來的郵箱是否存在,若存在則把賬號、密碼、郵箱返回給servlet
Login login = null;
Connection conn = (Connection) JDBCUtils.getConnection();
String sql="SELECT loginName,loginPwd,loginEmail FROM tbl_login WHERE loginEmail=?";
try {
PreparedStatement pre = (PreparedStatement) conn.prepareStatement(sql);
pre.setString(1, email);
ResultSet rs = pre.executeQuery();
if(rs.next()){
String loginName=rs.getString(1);
String loginPwd=rs.getString(2);
String loginEmail = rs.getString(3);
login = new Login(loginName,loginPwd,loginEmail);
return login;
}
else{
return login;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return login;
}
}
效果展示
上面我們只向郵箱裡發功了一個訊息,這種資訊不美觀,下面我們修改成向郵箱傳送一個頁面,其他操作都一樣我們只需要改一下發送資訊的內容
package src.com.haihang.email;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.activation.*;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendEmail
{
public static void main(String [] args)
{
String to = "[email protected]";
// 發件人電子郵箱
String from = "[email protected]";
// 指定傳送郵件的主機為 smtp.qq.com
String host = "smtp.qq.com"; //QQ 郵件伺服器
// 獲取系統屬性
Properties properties = System.getProperties();
// 設定郵件伺服器
properties.setProperty("mail.smtp.host", host);
properties.put("mail.smtp.auth", "true");
// 獲取預設session物件
Session session = Session.getDefaultInstance(properties,new Authenticator(){
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication("[email protected]", "kwifhodgdpbldigd"); //發件人郵件使用者名稱、授權碼
}
});
try{
// 建立預設的 MimeMessage 物件
MimeMessage message = new MimeMessage(session);
// Set From: 頭部頭欄位
message.setFrom(new InternetAddress(from));
// Set To: 頭部頭欄位
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
// Set Subject: 頭部頭欄位
message.setSubject("This is the Subject Line!");
// 設定訊息體
// message.setText("東哥最帥");
String msgContent = "<!DOCTYPE html>"
+"<html>"
+"<head>"
+"<meta charset='utf-8' />"
+"<title>歡迎使用員工管理系統</title>"
+"</head>"
+"<body>"
+"親愛的會員 ,您好,"
+"<br/><br/> "
+"您在"
+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ").format(new Date())
+"提交找回密碼的請求。"
+"<br/><br/>"
+"以下是您的帳戶及密碼資訊:"
+"<br/><br/>"
+ "使用者名稱:,密碼:"
+"<br/> <br/>"
+"感謝您使用本系統。"
+"<br/>"
+"此為自動傳送郵件,請勿直接回復!"
+"</body>"
+"</html>";
message.setContent(msgContent, "text/html;charset=utf-8");// 設定郵件內容,為html格式
// 傳送訊息
Transport.send(message);
System.out.println("Sent message successfully....from runoob.com");
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
效果展示