Before summarizing how to send mail in Java, Java implements database connection pool and Java Properties class, we have enough technical reserve to realize this requirement, now we need to be how to combine these technologies.
Requirements: Our users forget the password, need to send a password to change the link to his mailbox, users click on the link can be
To modify the password.
General ideas:
When a user requests a mailbox verification, generate a user name and the current time binding of the MD5 value, stored in the validation database, and the value of the concatenation of the authentication connection sent to the user's mailbox, when the user links and verify the database MD5 value, if the consistent give users the right to modify the password, Delete the data in the validation database at the same time.
Detailed design:
Using MVC Model
Database aspect:
Database: Mail
Table: Verify,student
When the user requests to reset the password, writes the data to the verify, when the mailbox verifies and verify, after verifying, adds the information to the student, deletes the verify contents.
Java: (partial)
Model:student.java, Md5.java, Random.java
Control:reset.java, Mailreset.java, Mailverify.java
View:reset.jsp, result.jsp, exception.jsp
Implementation: (Only the key code is posted)
Assuming that we have completed the connection pool construction, the mail has been sent to the configuration completed.
The database mentioned earlier:
The code is as follows |
Copy Code |
Transact-SQL Create database if not exists mail; Use mail; drop table if exists student; CREATE TABLE Student ( Stu_name varchar () primary key, Stu_password varchar (m) NOT NULL, Stu_email varchar (m) NOT NULL ); Use mail; drop table if exists verify; CREATE TABLE Verify ( Stu_name varchar () primary key, STU_NAMEMD5 varchar (m) NOT NULL, RandMd5 varchar (m) NOT NULL ); |
Then the connection to the Verify table and the operation to delete the MD5 value
The code is as follows |
Copy Code |
Op Con Java Package Bean; Import java.sql.*; public class Condb { Private Connection con; Private Statement STA; Private ResultSet Res; Private PreparedStatement pres; Public Condb () throws sqlexception,classnotfoundexception{ Class.forName ("Com.mysql.jdbc.Driver"); Con=drivermanager.getconnection ("Jdbc:mysql://localhost:3306/mail", "Root", "123456"); Sta=con.createstatement (); } public void Addstudenttemp (String stu_name,string stu_password,string stu_email) Throws sqlexception{ String sql= "INSERT into studenttemp values (?,?,?)"; Pres=con.preparestatement (SQL); Pres.setstring (1,stu_name); Pres.setstring (2,stu_password); Pres.setstring (3,stu_email); Pres.executeupdate (); } public void Addverify (String stu_name,string stu_namemd5,string randMd5) Throws sqlexception{ String sql= "INSERT into verify values (?,?,?)"; Pres=con.preparestatement (SQL); Pres.setstring (1,stu_name); Pres.setstring (2,STU_NAMEMD5); Pres.setstring (3,RANDMD5); Pres.executeupdate (); } public void Delverify (String stu_name) Throws sqlexception{ String sql= "Delete from verify where stu_name= '" +stu_name+ ""; Sta.executeupdate (SQL); } public string getverify (String stu_namemd5,string randMd5) Throws sqlexception{ String Stu_name=null; String sql= "Select Stu_name from verify where stu_namemd5=?" and randmd5=? "; Pres=con.preparestatement (SQL); Pres.setstring (1,STU_NAMEMD5); Pres.setstring (2,RANDMD5); Res=pres.executequery (); while (Res.next ()) { Stu_name=res.getstring ("Stu_name"); } return stu_name; } public void Addstudent (String stu_name) Throws sqlexception{ String sql= "INSERT INTO student select * from studenttemp where stu_name= '" +stu_name+ ""; Sta.executeupdate (SQL); } public string Getstu_name (string stu_email) Throws sqlexception{ String sql= "Select Stu_name from student where stu_email= '" +stu_email+ ""; String Stu_name=null; Res=sta.executequery (SQL); while (Res.next ()) { Stu_name=res.getstring ("Stu_name"); } return stu_name; } public void ResetPassword (String stu_name,string Stu_password) Throws sqlexception{ String sql= "Update student set stu_password=?" where stu_name=? "; Pres=con.preparestatement (SQL); Pres.setstring (1,stu_password); Pres.setstring (2,stu_name); Pres.executeupdate (); } public boolean existstudent (String stu_name,string Stu_password) Throws sqlexception{ Boolean exist=false; String sql= "SELECT * from student where stu_name=?" and stu_password=? "; Pres=con.preparestatement (SQL); Pres.setstring (1,stu_name); Pres.setstring (2,stu_password); Res=pres.executequery (); if (Res.next ()) { Exist=true; } return exist; } public void Addreset (String stu_name,string stu_namemd5, String randMd5) Throws SQLException { String sql= "INSERT into reset (STU_NAME,STU_NAMEMD5,RANDMD5) VALUES (?,?,?)"; Pres=con.preparestatement (SQL); Pres.setstring (1,stu_name); Pres.setstring (2,STU_NAMEMD5); Pres.setstring (3,RANDMD5); Pres.executeupdate (); } public void Delreset (String stu_name) Throws SQLException { String sql= "Delete from reset where stu_name= '" +stu_name+ ""; Sta.execute (SQL); } public string Getlegalreset (String stu_namemd5,string randMd5) Throws SQLException { String Stu_name=null; String sql= "Select Stu_name, Timestampdiff (Hour,sent_time,now ()) as hours from reset where stu_namemd5=?" and randmd5=? "; Pres=con.preparestatement (SQL); Pres.setstring (1,STU_NAMEMD5); Pres.setstring (2,RANDMD5); Res=pres.executequery (); if (Res.next ()) { Stu_name=res.getstring ("Stu_name"); int H=res.getint ("hours"); if (h<=24) { return stu_name; } } return stu_name; } public void Close () Throws SQLException { { if (res!= null) { Res.close (); } if (STA!= null) { Sta.close (); } if (con!= null) { Con.close (); con = null; } } } } |
To send a message that contains a constructed validation string:
The code is as follows |
Copy Code |
Sendverifymail Java Package Bean; Import java.util.*; Import javax.mail.Session; Import Javax.mail.Message; Import Javax.mail.internet.MimeMessage; Import javax.mail.internet.InternetAddress; Import Javax.mail.Message.RecipientType; Import Javax.mail.Transport; Import javax.mail.MessagingException; public class SendMail { String username= "dengzhaoqun@163.com"; Private Message GetMessage () { Properties P=new properties (); P.put ("Mail.transport.protocol", "SMTP"); P.put ("Mail.smtp.host", "smtp.163.com"); P.put ("Mail.smtp.port", "25"); P.put ("Mail.smtp.auth", "true"); String password= "20083111tian"; MyAuthor auth=new MyAuthor (Username,password); Session session=session.getdefaultinstance (P,auth); Message Message=new MimeMessage (session); return message; } public void Sendverify (String stu_email,string stu_namemd5,string randMd5) Throws Messagingexception { Message Message=getmessage (); Message.setfrom (new internetaddress (username)); Message.setrecipient (recipienttype.to,new internetaddress (stu_email)); Message.setsentdate (New Date ()); Message.setsubject ("mailverify"); String m= "<a href=" http://127.0.01:8080/Mail/mailVerify?stu_nameMd5= "+stu_namemd5+" &randmd5= "+randmd5+" " > "+ "Http:/127.0.01:8080/mail/mailverify?stu_namemd5=" +stu_namemd5+ "&randmd5=" +randmd5+ "</a>"; Message.setcontent (M, "text/html;charset=gb2312"); Transport.send (message); } public void Sendreset (String stu_email,string stu_namemd5,string randMd5) Throws Messagingexception { Message Message=getmessage (); Message.setfrom (new internetaddress (username)); Message.setrecipient (recipienttype.to,new internetaddress (stu_email)); Message.setsentdate (New Date ()); Message.setsubject ("mailverify"); String m= "<a href=" http://127.0.01:8080/Mail/mailReset?stu_nameMd5= "+stu_namemd5+" &randmd5= "+randmd5+" " > "+ "Http://127.0.01:8080/Mail/mailReset?stu_nameMd5=" +stu_namemd5+ "&randmd5=" +randmd5+ "</a>"; Message.setcontent (M, "text/html;charset=gb2312"); Transport.send (message); } } |
To validate incoming data:
The code is as follows |
Copy Code |
Mailverify Java Package servlet; Import java.io.IOException; Import javax.servlet.http.*; Import javax.servlet.ServletException; Import java.sql.SQLException; Import bean.*; public class Mailverify extends HttpServlet { public void doget (HttpServletRequest request, HttpServletResponse response) Throws Servletexception,ioexception { DoPost (Request,response); } public void DoPost (HttpServletRequest request, HttpServletResponse response) Throws Servletexception,ioexception { Condb con; String Stu_name; String msg; HttpSession session=request.getsession (); String stu_namemd5=request.getparameter ("Stu_namemd5"); String randmd5=request.getparameter ("RandMd5"); try { Con=new condb (); Stu_name=con.getverify (STU_NAMEMD5,RANDMD5); if (stu_name!=null) { Msg= "Registration is successful, please return to the login page"; Con.addstudent (Stu_name); Con.delstudenttemp (Stu_name); Con.delverify (Stu_name); }else{msg= "Error";} Con.close (); Session.setattribute ("msg", MSG); Response.sendredirect ("result.jsp"); }catch (ClassNotFoundException e) { throw new Servletexception (E.fillinstacktrace ()); }catch (SQLException e) { throw new Servletexception (E.fillinstacktrace ()); } } } |
If the checksum succeeds, insert the modified data into the database (critical method):
The code is as follows |
Copy Code |
Resetinfo Java public void Delverify (String stu_name) Throws sqlexception{ String sql= "Delete from verify where stu_name= '" +stu_name+ ""; Sta.executeupdate (SQL); } public string getverify (String stu_namemd5,string randMd5) Throws sqlexception{ String Stu_name=null; String sql= "Select Stu_name from verify where stu_namemd5=?" and randmd5=? "; Pres=con.preparestatement (SQL); Pres.setstring (1,STU_NAMEMD5); Pres.setstring (2,RANDMD5); Res=pres.executequery (); while (Res.next ()) { Stu_name=res.getstring ("Stu_name"); } return stu_name; } public void Addstudent (String stu_name) Throws sqlexception{ String sql= "INSERT INTO student select * from studenttemp where stu_name= '" +stu_name+ ""; Sta.executeupdate (SQL); } public string Getstu_name (string stu_email) Throws sqlexception{ String sql= "Select Stu_name from student where stu_email= '" +stu_email+ ""; String Stu_name=null; Res=sta.executequery (SQL); while (Res.next ()) { Stu_name=res.getstring ("Stu_name"); } return stu_name; } public void ResetPassword (String stu_name,string Stu_password) Throws sqlexception{ String sql= "Update student set stu_password=?" where stu_name=? "; Pres=con.preparestatement (SQL); Pres.setstring (1,stu_password); Pres.setstring (2,stu_name); Pres.executeupdate (); } public boolean existstudent (String stu_name,string Stu_password) Throws sqlexception{ Boolean exist=false; String sql= "SELECT * from student where stu_name=?" and stu_password=? "; Pres=con.preparestatement (SQL); Pres.setstring (1,stu_name); Pres.setstring (2,stu_password); Res=pres.executequery (); if (Res.next ()) { Exist=true; } return exist; } |
This will enable the verification of the function of the message