JavaWeb利用郵箱幫用戶(hù)找回密碼
本文是介紹在一個(gè)小的JAVAWeb項(xiàng)目中,利用郵箱幫用戶(hù)找回密碼。
效果展示

需要一個(gè)發(fā)送郵件的jar包 : javax.mail .jar
1.JSP頁(yè)面(設(shè)置郵箱輸入框)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>找回密碼-圖書(shū)管理系統(tǒng)</title>
<link rel="stylesheet" href="bootstrap/css/bootstrap.min.css" rel="external nofollow" >
<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="請(qǐng)輸入您的郵箱地址"
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">返回登錄頁(yè)面</button>
</div>
<script type="text/javascript">
function backLogin() {
window.location.href = "login.jsp"
}
</script>
</body>
</html>
2.Servlet代碼(根據(jù)用戶(hù)輸入的郵箱賬號(hào)找到用戶(hù),并生成發(fā)送郵件類(lèi)的實(shí)例,再設(shè)置收件人和要發(fā)送的內(nèi)容,最后發(fā)送郵件)
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//獲取用戶(hù)的郵箱
String email = request.getParameter("email");
Admin admin = null;
User user = null;
PrintWriter out = response.getWriter();
//實(shí)例化一個(gè)發(fā)送郵件的對(duì)象
SendMail mySendMail = new SendMail();
//根據(jù)郵箱找到該用戶(hù)信息
admin = adminService.getAdminByEmail(email);
if(admin!=null) {
//設(shè)置收件人和消息內(nèi)容
mySendMail.sendMail(email, "圖書(shū)管理系統(tǒng)提醒,您的密碼為:"+admin.getPassword());
out.println("<script>alert('恭喜,找回密碼成功');window.location.href='login.jsp'</script>");
} else {
user = userService.getUserByEmail(email);
if(user!=null) {
mySendMail.sendMail(email, "圖書(shū)管理系統(tǒng)提醒,您的密碼為:"+user.getPassword());
out.println("<script>alert('恭喜,找回密碼成功');window.location.href='login.jsp'</script>");
}
}
out.println("<script>alert('該郵箱尚未注冊(cè)!請(qǐng)重新輸入');window.location.href='retrievePassword.jsp'</script>");
}
3.發(fā)送郵件類(lèi)
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 {
// 發(fā)件人的郵箱賬號(hào)如:xxx@163.com
public static String sendEmailAccount = "";
// 發(fā)件人的郵箱的授權(quán)碼(自己在郵箱服務(wù)器中開(kāi)啟并設(shè)置)
public static String sendEmailPassword = "";
// 發(fā)件人郵箱的SMTP服務(wù)器地址,如:smtp.163.com
public static String sendEmailSMTPHost = "smtp.163.com";
// 收件人的郵箱賬號(hào)
public static String receiveMailAccount = "";
// 把發(fā)送郵件封裝為函數(shù),參數(shù)為收件人的郵箱賬號(hào)和要發(fā)送的內(nèi)容
public void sendMail(String receiveMailAccount, String mailContent) {
// 創(chuàng)建用于連接郵件服務(wù)器的參數(shù)配置
Properties props = new Properties();
// 設(shè)置使用SMTP協(xié)議
props.setProperty("mail.transport.protocol", "smtp");
// 設(shè)置發(fā)件人的SMTP服務(wù)器地址
props.setProperty("mail.smtp.host", sendEmailSMTPHost);
// 設(shè)置需要驗(yàn)證
props.setProperty("mail.smtp.auth", "true");
// 根據(jù)配置創(chuàng)建會(huì)話(huà)對(duì)象, 用于和郵件服務(wù)器交互
Session session = Session.getInstance(props);
// 設(shè)置debug模式,便于查看發(fā)送過(guò)程所產(chǎn)生的日志
session.setDebug(true);
try {
// 創(chuàng)建一封郵件
MimeMessage message = createMimeMessage(session, sendEmailAccount, receiveMailAccount, mailContent);
// 根據(jù) Session 獲取郵件傳輸對(duì)象
Transport transport = session.getTransport();
transport.connect(sendEmailAccount, sendEmailPassword);
// 發(fā)送郵件, 發(fā)到所有的收件地址, 通過(guò)message.getAllRecipients() 可以獲取到在創(chuàng)建郵件對(duì)象時(shí)添加的所有收件人
transport.sendMessage(message, message.getAllRecipients());
// 關(guān)閉連接
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
* 和服務(wù)器交互的會(huì)話(huà)
* @param sendMail
* 發(fā)件人郵箱
* @param receiveMail
* 收件人郵箱
* @return
* @throws Exception
*/
public static MimeMessage createMimeMessage(Session session, String sendMail, String receiveMail,
String mailContent) throws Exception {
// 創(chuàng)建一封郵件
MimeMessage message = new MimeMessage(session);
// 設(shè)置發(fā)件人姓名和編碼格式
message.setFrom(new InternetAddress(sendMail, "圖書(shū)管理系統(tǒng)", "UTF-8"));
// 收件人
message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(receiveMail, "尊敬的用戶(hù)", "UTF-8"));
// 設(shè)置郵件主題
message.setSubject("找回密碼提醒", "UTF-8");
// 設(shè)置郵件正文
message.setContent(mailContent, "text/html;charset=UTF-8");
// 設(shè)置發(fā)件時(shí)間
message.setSentDate(new Date());
// 保存設(shè)置
message.saveChanges();
return message;
}
}
注意此處用的授權(quán)碼,需要自己登錄郵箱去設(shè)置,如163郵箱設(shè)置如下:


以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
spring?@Conditional的使用與擴(kuò)展源碼分析
這篇文章主要介紹了spring?@Conditional的使用與擴(kuò)展,這里需要注意如果Condition返回的是false,那么spirng就不會(huì)對(duì)方法或類(lèi)進(jìn)行解析,具體源碼分析跟隨小編一起看看吧2022-03-03
java實(shí)現(xiàn)在SSM下使用支付寶掃碼支付功能
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)在SSM下使用支付寶掃碼支付功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02
Python爬蟲(chóng) 12306搶票開(kāi)源代碼過(guò)程詳解
這篇文章主要介紹了Python爬蟲(chóng) 12306搶票開(kāi)源代碼過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09
詳解SpringBoot中@SessionAttributes的使用
這篇文章主要通過(guò)示例為大家詳細(xì)介紹了SpringBoot中@SessionAttributes的使用,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-07-07
Java中Integer.valueOf,parsetInt() String.valueOf的區(qū)別和結(jié)果代碼解析
本文通過(guò)代碼給大家講解了JAVA中Integer.valueOf, parsetInt() String.valueOf的區(qū)別和結(jié)果,需要的朋友可以參考下2018-05-05
Maven遠(yuǎn)程倉(cāng)庫(kù)地址修改實(shí)現(xiàn)解析
這篇文章主要介紹了Maven遠(yuǎn)程倉(cāng)庫(kù)地址修改實(shí)現(xiàn)解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
MyBatis實(shí)現(xiàn)樂(lè)觀(guān)鎖和悲觀(guān)鎖的示例代碼
在數(shù)據(jù)庫(kù)操作中,樂(lè)觀(guān)鎖和悲觀(guān)鎖是兩種常見(jiàn)的并發(fā)控制策略,本文主要介紹了MyBatis實(shí)現(xiàn)樂(lè)觀(guān)鎖和悲觀(guān)鎖的示例代碼,具有一定的參考價(jià)值,感興趣的可以了解一下2024-07-07
SPRINGBOOT讀取PROPERTIES配置文件數(shù)據(jù)過(guò)程詳解
這篇文章主要介紹了SPRINGBOOT讀取PROPERTIES配置文件數(shù)據(jù)過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12

