JavaWeb利用郵箱幫用戶找回密碼
更新時間:2019年02月10日 10:51:20 作者:學以致用HT
這篇文章主要為大家詳細介紹了JavaWeb利用郵箱幫用戶找回密碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文是介紹在一個小的JAVAWeb項目中,利用郵箱幫用戶找回密碼。
效果展示
需要一個發(fā)送郵件的jar包 : javax.mail .jar
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>找回密碼-圖書管理系統(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="請輸入您的郵箱地址" 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代碼(根據用戶輸入的郵箱賬號找到用戶,并生成發(fā)送郵件類的實例,再設置收件人和要發(fā)送的內容,最后發(fā)送郵件)
/** * @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(); //實例化一個發(fā)送郵件的對象 SendMail mySendMail = new SendMail(); //根據郵箱找到該用戶信息 admin = adminService.getAdminByEmail(email); if(admin!=null) { //設置收件人和消息內容 mySendMail.sendMail(email, "圖書管理系統(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, "圖書管理系統(tǒng)提醒,您的密碼為:"+user.getPassword()); out.println("<script>alert('恭喜,找回密碼成功');window.location.href='login.jsp'</script>"); } } out.println("<script>alert('該郵箱尚未注冊!請重新輸入');window.location.href='retrievePassword.jsp'</script>"); }
3.發(fā)送郵件類
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ā)件人的郵箱賬號如:xxx@163.com public static String sendEmailAccount = ""; // 發(fā)件人的郵箱的授權碼(自己在郵箱服務器中開啟并設置) public static String sendEmailPassword = ""; // 發(fā)件人郵箱的SMTP服務器地址,如:smtp.163.com public static String sendEmailSMTPHost = "smtp.163.com"; // 收件人的郵箱賬號 public static String receiveMailAccount = ""; // 把發(fā)送郵件封裝為函數,參數為收件人的郵箱賬號和要發(fā)送的內容 public void sendMail(String receiveMailAccount, String mailContent) { // 創(chuàng)建用于連接郵件服務器的參數配置 Properties props = new Properties(); // 設置使用SMTP協議 props.setProperty("mail.transport.protocol", "smtp"); // 設置發(fā)件人的SMTP服務器地址 props.setProperty("mail.smtp.host", sendEmailSMTPHost); // 設置需要驗證 props.setProperty("mail.smtp.auth", "true"); // 根據配置創(chuàng)建會話對象, 用于和郵件服務器交互 Session session = Session.getInstance(props); // 設置debug模式,便于查看發(fā)送過程所產生的日志 session.setDebug(true); try { // 創(chuàng)建一封郵件 MimeMessage message = createMimeMessage(session, sendEmailAccount, receiveMailAccount, mailContent); // 根據 Session 獲取郵件傳輸對象 Transport transport = session.getTransport(); transport.connect(sendEmailAccount, sendEmailPassword); // 發(fā)送郵件, 發(fā)到所有的收件地址, 通過message.getAllRecipients() 可以獲取到在創(chuàng)建郵件對象時添加的所有收件人 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 * 發(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); // 設置發(fā)件人姓名和編碼格式 message.setFrom(new InternetAddress(sendMail, "圖書管理系統(tǒng)", "UTF-8")); // 收件人 message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(receiveMail, "尊敬的用戶", "UTF-8")); // 設置郵件主題 message.setSubject("找回密碼提醒", "UTF-8"); // 設置郵件正文 message.setContent(mailContent, "text/html;charset=UTF-8"); // 設置發(fā)件時間 message.setSentDate(new Date()); // 保存設置 message.saveChanges(); return message; } }
注意此處用的授權碼,需要自己登錄郵箱去設置,如163郵箱設置如下:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
詳解SpringBoot中@SessionAttributes的使用
這篇文章主要通過示例為大家詳細介紹了SpringBoot中@SessionAttributes的使用,文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下2022-07-07Java中Integer.valueOf,parsetInt() String.valueOf的區(qū)別和結果代碼解析
本文通過代碼給大家講解了JAVA中Integer.valueOf, parsetInt() String.valueOf的區(qū)別和結果,需要的朋友可以參考下2018-05-05SPRINGBOOT讀取PROPERTIES配置文件數據過程詳解
這篇文章主要介紹了SPRINGBOOT讀取PROPERTIES配置文件數據過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-12-12