本文是介紹在一個小的javaweb項目中,利用郵箱幫用戶找回密碼。
效果展示
需要一個發送郵件的jar包 : javax.mail .jar
1.jsp頁面(設置郵箱輸入框)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
<%@ 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" 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代碼(根據用戶輸入的郵箱賬號找到用戶,并生成發送郵件類的實例,再設置收件人和要發送的內容,最后發送郵件)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
/** * @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.發送郵件類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
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 { // 發件人的郵箱賬號如:xxx@163.com 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郵箱設置如下:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/qq_40348465/article/details/83629000