JavaWeb利用邮箱帮用户找回密码
程序员文章站
2024-02-21 11:19:52
本文是介绍在一个小的javaweb项目中,利用邮箱帮用户找回密码。
效果展示
需要一个发送邮件的jar包 : java...
本文是介绍在一个小的javaweb项目中,利用邮箱帮用户找回密码。
效果展示
需要一个发送邮件的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>找回密码-图书管理系统</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代码(根据用户输入的邮箱账号找到用户,并生成发送邮件类的实例,再设置收件人和要发送的内容,最后发送邮件)
/** * @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 { // 发件人的邮箱账号如: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邮箱设置如下:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。