JavaWeb QQ邮箱找回密码

我的上一篇博客,已经写了登录注册,接下来写QQ邮箱找回密码

首先:我们需要在 QQ邮箱设置中获取授权码,步骤如下

进入QQ邮箱---->设置---->账户---->开启服务:POP3/SMTP服务---->保存授权码

在这里插入图片描述

在这里插入图片描述

**其次:**我百度云盘里保存了可以提取,也可以百度下载
链接:https://pan.baidu.com/s/1IT_1sDNvALVh-H_78nMXaA
提取码:btnb
导入项目中 或者
在这里插入图片描述

**最后:**代码展示

1.SendEmial.jsp(前端页面)

<form name="f1" id="f1" action="sedEmail" method="post">
		<table border="0">
			<tr>
				<td colspan="1">
				   <center>
						<h3>邮箱找回密码</h3>
				   </center>
				</td>
			</tr>
			<tr>
				<td>
				   <input type="text" name="email" id="email" placeholder="请输入您的邮箱号">					
				</td>
				<td colspan="1">
				   <center>
						<font color="red" size="2"> ${MSG}</font>
				   </center>
				</td>
			</tr>
			<tr>
				<td>
				    <input type="submit" value="确认">
				</td>
				<td colspan="1">
				   <center>
						<font color="red" size="2"> ${MSG3}</font>
				   </center>
				</td>
			</tr>
		</table>
	</form>
	<a href="login.jsp" style="margin-left: 70px;"><font size="2"><i>返回登录</i>
	</font> </a>

2.CheckSendEmail.java(servlet进行数据处理,发送邮件)

package com.aiit.service;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.aiit.dao.SendEmail;
import com.aiit.model.Login;
@WebServlet("/sedEmail")
public class CheckSendEmail extends HttpServlet {

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//接收页面传过来的QQ邮箱号
       String email  = request.getParameter("email");
       //传到数据库,判断是否存在,如果存在,返回用户账号信息
       SendEmail send = new SendEmail();
          Login login = send.getEmail(email);
          //System.out.println(login.getEmail());
          //to接收的是用户注册时的邮箱号,也就是就是收件人的,将接收到后台发送的密码
          String to = login.getEmail();
  	    
	      // 发件人电子邮箱,你可以改成自己的邮箱号
	      String from = "********@qq.com";
	 
	      // 指定发送邮件的主机为 smtp.qq.com
	      String host = "smtp.qq.com";  //QQ 邮件服务器
	 
	      // 获取系统属性
	      Properties properties = System.getProperties(); 
	      // 设置邮件服务器
	      properties.setProperty("mail.smtp.host", host); 
	      properties.put("mail.smtp.auth", "true");
	      // 获取默认session对象
	      Session session = Session.getDefaultInstance(properties,new Authenticator(){
	        public PasswordAuthentication getPasswordAuthentication()
	        {
	         return new PasswordAuthentication("********@qq.com", "kwifhodgdpbldigd"); 
	          //发件人邮件用户名、授权码(授权码要与QQ邮箱相对应,可以从邮箱设置里面获得,详细步骤在博客开头)
	        }
	       });	 
	      try{
	         // 创建默认的 MimeMessage 对象
	         MimeMessage message = new MimeMessage(session);	 
	         // Set From: 头部头字段
	         message.setFrom(new InternetAddress(from));	 
	         // Set To: 头部头字段
	         message.addRecipient(Message.RecipientType.TO,
	                                  new InternetAddress(to));
	 
	         // Set Subject: 头部头字段
	         message.setSubject("This is the Subject Line!");
	 
	         // 设置消息体
	         message.setText("您的员工管理系统,密码是:"+login.getLoginPwd());
	 
	         // 发送消息
	         Transport.send(message);
	         //System.out.println("Sent message successfully....from runoob.com");
	         //传到页面
	     	request.setAttribute("MSG3", "发送成功,请注意查收!");
	         request.getRequestDispatcher("SendEmial.jsp").forward(request, response);
	      }catch (MessagingException mex) {
	         mex.printStackTrace();
	      }	
	}
}

3.SendEmail.java(数据库)


package com.aiit.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.aiit.common.JDBCUtils;
import com.aiit.model.Login;


public class SendEmail {

	public Login getEmail(String email){
		//先查找前端页面传过来的邮箱是否存在,若存在则把账号、密码、邮箱返回给servlet
		Login login = null;
		Connection conn = (Connection) JDBCUtils.getConnection();
		String sql="SELECT loginName,loginPwd,loginEmail  FROM tbl_login WHERE loginEmail=?";
		try {
			PreparedStatement pre = (PreparedStatement) conn.prepareStatement(sql);
			pre.setString(1, email);
			ResultSet rs = pre.executeQuery();
			if(rs.next()){
				String loginName=rs.getString(1);
				String loginPwd=rs.getString(2);
				String loginEmail = rs.getString(3);			
				login = new Login(loginName,loginPwd,loginEmail);	
				 return login;

			}
			else{
				 return login;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		return login;
		}	
}

效果展示
在这里插入图片描述在这里插入图片描述

上面我们只向邮箱里发功了一个消息,这种信息不美观,下面我们修改成向邮箱发送一个页面,其他操作都一样我们只需要改一下发送信息的内容

package src.com.haihang.email;

import java.text.SimpleDateFormat;
import java.util.*;
import javax.activation.*;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendEmail
{
	public static void main(String [] args)
	{   
		String to = "2053696385@qq.com";

		// 发件人电子邮箱
		String from = "2053696385@qq.com";

		// 指定发送邮件的主机为 smtp.qq.com
		String host = "smtp.qq.com";  //QQ 邮件服务器

		// 获取系统属性
		Properties properties = System.getProperties();

		// 设置邮件服务器
		properties.setProperty("mail.smtp.host", host);

		properties.put("mail.smtp.auth", "true");
		// 获取默认session对象
		Session session = Session.getDefaultInstance(properties,new Authenticator(){
			public PasswordAuthentication getPasswordAuthentication()
			{
				return new PasswordAuthentication("2053696385@qq.com", "kwifhodgdpbldigd"); //发件人邮件用户名、授权码
			}
		});

		try{
			// 创建默认的 MimeMessage 对象
			MimeMessage message = new MimeMessage(session);

			// Set From: 头部头字段
			message.setFrom(new InternetAddress(from));

			// Set To: 头部头字段
			message.addRecipient(Message.RecipientType.TO,
					new InternetAddress(to));

			// Set Subject: 头部头字段
			message.setSubject("This is the Subject Line!");

			// 设置消息体
			// message.setText("东哥最帅");

			String msgContent = "<!DOCTYPE html>"
					+"<html>"
					+"<head>"
					+"<meta charset='utf-8' />"
					+"<title>欢迎使用员工管理系统</title>"
					+"</head>"
					+"<body>"
							+"亲爱的会员 ,您好,"
							+"<br/><br/> " 
							+"您在" 
							+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ").format(new Date())
							+"提交找回密码的请求。"
							+"<br/><br/>"
							+"以下是您的帐户及密码信息:"
							+"<br/><br/>"
							+ "用户名:,密码:"
							+"<br/> <br/>"
							+"感谢您使用本系统。"
							+"<br/>"
							+"此为自动发送邮件,请勿直接回复!"
							+"</body>"
							+"</html>";  
			message.setContent(msgContent, "text/html;charset=utf-8");// 设置邮件内容,为html格式 

			// 发送消息
			Transport.send(message);
			System.out.println("Sent message successfully....from runoob.com");
		}catch (MessagingException mex) {
			mex.printStackTrace();
		}
	}
}

效果展示
在这里插入图片描述

  • 4
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
为了实现JavaWeb找回密码功能,可以通过邮箱发送验证码的方式来实现。具体步骤如下: 1. 用户在找回密码页面输入注册时使用的邮箱地址,并点击发送验证码按钮。 2. 后台接收到请求后,生成一个随机的验证码,并将其存储在服务器端的缓存中,同时将验证码发送到用户的邮箱中。 3. 用户在邮箱中收到验证码后,将其输入到找回密码页面中,并点击验证按钮。 4. 后台接收到请求后,从服务器端的缓存中获取之前存储的验证码,并与用户输入的验证码进行比对。 5. 如果验证码匹配成功,则生成一个随机的密码,并将其发送到用户的邮箱中。 6. 用户在邮箱中收到新密码后,即可使用新密码登录系统。 以下是一个简单的JavaWeb找回密码的示例代码: ```java // 生成随机验证码 String code = String.valueOf(new Random().nextInt(899999) + 100000); // 将验证码存储在服务器端的缓存中 request.getSession().setAttribute("code", code); // 发送验证码到用户的邮箱中 String to = "user@example.com"; String subject = "找回密码验证码"; String content = "您的验证码为:" + code; MailUtils.sendMail(to, subject, content); // 验证用户输入的验证码 String inputCode = request.getParameter("code"); String codeInSession = (String) request.getSession().getAttribute("code"); if (inputCode.equals(codeInSession)) { // 生成随机密码 String password = String.valueOf(new Random().nextInt(899999) + 100000); // 将新密码发送到用户的邮箱中 String to = "user@example.com"; String subject = "新密码"; String content = "您的新密码为:" + password; MailUtils.sendMail(to, subject, content); } else { // 验证码不匹配,提示用户重新输入 response.getWriter().write("验证码错误,请重新输入!"); } ```
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值