本文是介绍在一个小的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 width="300px" height="200px"> <form action="RetrievePassword.do" method="post"> <input type="email" name="email" id="email" width="100px" height="60px" 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邮箱设置如下:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。