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 = "[email protected]";

		// 发件人电子邮箱
		String from = "[email protected]";

		// 指定发送邮件的主机为 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("[email protected]", "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();
		}
	}
}

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

猜你喜欢

转载自blog.csdn.net/qq_38720976/article/details/83506210
今日推荐