1. 程式人生 > >JAVAWeb中實現簡單郵箱找回密碼

JAVAWeb中實現簡單郵箱找回密碼

  版權宣告:本文為博主原創文章,未經博主允許不得轉載。https://blog.csdn.net/qq_40348465/article/details/83629000

本文是介紹在一個小的JAVAWeb專案中,利用郵箱幫使用者找回密碼。

  效果展示   

  

 

1.JSP頁面(設定郵箱輸入框)

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>找回密碼-圖書管理系統</title>
<link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
<script type="text/javascript" src="bootstrap/js/bootstrap.min.js"></script>
</head>
<body>
	<div style="text-align: center" width="300px" height="200px">
		<form action="RetrievePassword.do" method="post">


			<input type="email" name="email" id="email" width="100px"
				height="60px" style="margin-top: 100px" placeholder="請輸入您的郵箱地址"
				required> <br>
			<br>

			<button type="submit" class="btn btn-success" id="button"
				width="100px" height="60px">找回密碼</button>

		</form>
		<br>
		<br>

		<button type="button" class="btn btn-primary" id="button"
			onclick="backLogin()" width="100px" height="60px">返回登入頁面</button>
	</div>
	<script type="text/javascript">
		function backLogin() {
			window.location.href = "login.jsp"
		}
	</script>
</body>
</html>

 

 2.Servlet程式碼(根據使用者輸入的郵箱賬號找到使用者,並生成傳送郵件類的例項,再設定收件人和要傳送的內容,最後傳送郵件)

/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//獲取使用者的郵箱
		String email = request.getParameter("email");
		Admin admin = null;
		User user = null;
		PrintWriter out = response.getWriter();
		//例項化一個傳送郵件的物件
		SendMail mySendMail = new SendMail();
		//根據郵箱找到該使用者資訊
		admin = adminService.getAdminByEmail(email);
		if(admin!=null) {
			//設定收件人和訊息內容
			mySendMail.sendMail(email, "圖書管理系統提醒,您的密碼為:"+admin.getPassword());
			out.println("<script>alert('恭喜,找回密碼成功');window.location.href='login.jsp'</script>");
		} else {
			user = userService.getUserByEmail(email);
			if(user!=null) {
				mySendMail.sendMail(email, "圖書管理系統提醒,您的密碼為:"+user.getPassword());
				out.println("<script>alert('恭喜,找回密碼成功');window.location.href='login.jsp'</script>");
			}
		}
		out.println("<script>alert('該郵箱尚未註冊!請重新輸入');window.location.href='retrievePassword.jsp'</script>");	
	}

 

3.傳送郵件類

package com.bookms.util;

import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.Properties;

public class SendMail {

	// 發件人的郵箱賬號如:
[email protected]
public static String sendEmailAccount = ""; // 發件人的郵箱的授權碼(自己在郵箱伺服器中開啟並設定) public static String sendEmailPassword = ""; // 發件人郵箱的SMTP伺服器地址,如:smtp.163.com public static String sendEmailSMTPHost = "smtp.163.com"; // 收件人的郵箱賬號 public static String receiveMailAccount = ""; // 把傳送郵件封裝為函式,引數為收件人的郵箱賬號和要傳送的內容 public void sendMail(String receiveMailAccount, String mailContent) { // 建立用於連線郵件伺服器的引數配置 Properties props = new Properties(); // 設定使用SMTP協議 props.setProperty("mail.transport.protocol", "smtp"); // 設定發件人的SMTP伺服器地址 props.setProperty("mail.smtp.host", sendEmailSMTPHost); // 設定需要驗證 props.setProperty("mail.smtp.auth", "true"); // 根據配置建立會話物件, 用於和郵件伺服器互動 Session session = Session.getInstance(props); // 設定debug模式,便於檢視傳送過程所產生的日誌 session.setDebug(true); try { // 建立一封郵件 MimeMessage message = createMimeMessage(session, sendEmailAccount, receiveMailAccount, mailContent); // 根據 Session 獲取郵件傳輸物件 Transport transport = session.getTransport(); transport.connect(sendEmailAccount, sendEmailPassword); // 傳送郵件, 發到所有的收件地址, 通過message.getAllRecipients() 可以獲取到在建立郵件物件時新增的所有收件人 transport.sendMessage(message, message.getAllRecipients()); // 關閉連線 transport.close(); } catch (NoSuchProviderException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (MessagingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * * @param session * 和伺服器互動的會話 * @param sendMail * 發件人郵箱 * @param receiveMail * 收件人郵箱 * @return * @throws Exception */ public static MimeMessage createMimeMessage(Session session, String sendMail, String receiveMail, String mailContent) throws Exception { // 建立一封郵件 MimeMessage message = new MimeMessage(session); // 設定發件人姓名和編碼格式 message.setFrom(new InternetAddress(sendMail, "圖書管理系統", "UTF-8")); // 收件人 message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(receiveMail, "尊敬的使用者", "UTF-8")); // 設定郵件主題 message.setSubject("找回密碼提醒", "UTF-8"); // 設定郵件正文 message.setContent(mailContent, "text/html;charset=UTF-8"); // 設定發件時間 message.setSentDate(new Date()); // 儲存設定 message.saveChanges(); return message; } }

  注意此處用的授權碼,需要自己登入郵箱去設定,如163郵箱設定如下: