将已经保存在数据库中的密码通过邮件发送到qq邮箱中。用的ssm框架,其中的config文件要先配置好。
用到的jar包有gson-2.2.1.jar,gson.jar,mail.jar,activation.jar
1.entity类(WmUser.java)
public class WmUser {
private String userName;
private String userPsw;
private String email;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPsw() {
return userPsw;
}
public void setUserPsw(String userPsw) {
this.userPsw = userPsw;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
} public WmUser( String userName, String userPsw,
String email) {
super();
this.userName = userName;
this.userPsw = userPsw;
this.email = email;
}
public WmUser() {
super();
// TODO Auto-generated constructor stub
} }
2.dao层
UserMapper.java,是一个接口
public interface UserMapper {
//根据用户名和邮箱查找用户密码
public List<WmUser> findAllUser(WmUser wmUser);
}
UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.etc.dao.UserMapper" >
<!-- 查找密码 -->
<select id="findAllUser" parameterType="com.etc.entity.WmUser" resultType="com.etc.entity.WmUser" >
select userPsw from wm_user where userName=#{userName} and email=#{email}
</select> </mapper>
3.Biz层
UserBiz.java(接口)
public interface UserBiz {
//找回密码
public List<WmUser> findAllUser(WmUser wmUser);
}
UserBizImpl.java
import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import com.etc.biz.UserBiz;
import com.etc.dao.UserMapper;
import com.etc.entity.WmUser; public class UserBizImpl implements UserBiz {
@Autowired
UserMapper userMapper; //查找密码
public List<WmUser> findAllUser(WmUser wmUser) {
List<WmUser> user=userMapper.findAllUser(wmUser);
return user;
}
}
4.controller层
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties; import javax.mail.Authenticator;
import javax.mail.Message.RecipientType;
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.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; import com.etc.biz.UserBiz;
import com.etc.entity.WmUser;
import com.google.gson.Gson; @Controller
public class UserController {
private static final Logger logger=LoggerFactory.getLogger(CompanyController.class);
private static List<WmUser> list = new ArrayList<WmUser>();
@Autowired
UserBiz userBiz; //修改密码
@RequestMapping("/modPsw.action")
public void modPsw(HttpServletRequest request, HttpServletResponse response,WmUser wmUser)
throws Exception {
request.setCharacterEncoding("utf-8");
response.setContentType("application/json;charset=utf-8");
PrintWriter out = response.getWriter();
//获取从页面传递过来的用户名和邮箱
String userName=request.getParameter("userName2");
String email=request.getParameter("email1");
wmUser.setUserName(userName);
wmUser.setEmail(email);
list=userBiz.findAllUser(wmUser);
Gson gson = new Gson();
//获取到从数据库中查询出来的密码
String str = gson.toJson(list.get(0).getUserPsw()); //发送邮件
// 创建Properties 类用于记录邮箱的一些属性
final Properties props = new Properties();
// 表示SMTP发送邮件,必须进行身份验证
props.put("mail.smtp.auth", "true");
//此处填写SMTP服务器
props.put("mail.smtp.host", "smtp.qq.com");
//端口号,QQ邮箱给出了两个端口
props.put("mail.smtp.port", "587");
// 此处填写发件人的账号(qq邮箱)
props.put("mail.user", "发件人邮箱");
// 此处的密码就是前面说的16位STMP口令
props.put("mail.password", "在邮箱的账号中STMP授权码"); // 构建授权信息,用于进行SMTP进行身份验证
Authenticator authenticator = new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() {
// 用户名、密码
String userName = props.getProperty("mail.user");
String password = props.getProperty("mail.password");
return new PasswordAuthentication(userName, password);
}
}; //生成系统当前时间
Date date = new Date(); //format对象是用来以指定的时间格式格式化时间的
SimpleDateFormat from = new SimpleDateFormat( "yyyy年MM月dd日 HH时mm分ss秒"); //这里的格式可以自己设置
//format()方法是用来格式化时间的方法
String times = from.format(date); // 使用环境属性和授权信息,创建邮件会话
Session mailSession = Session.getInstance(props, authenticator);
// 创建邮件消息
MimeMessage message = new MimeMessage(mailSession);
// 设置发件人
InternetAddress form = new InternetAddress(
props.getProperty("mail.user"));
message.setFrom(form); // 设置收件人的邮箱
InternetAddress to = new InternetAddress("收件人邮箱");
message.setRecipient(RecipientType.TO, to); // 设置邮件标题
message.setSubject("修改密码"); // 设置邮件的内容体
message.setContent("尊敬的"+userName+"用户,您好!\r\n您在"+times+"提交找回密码请求,您的新密码为"+str+",请您登录后重新修改密码。\n如果您没有进行过找回密码的操作,请不要进行任何操作,并删除此邮件。谢谢!", "text/html;charset=UTF-8"); // 最后当然就是发送邮件啦
Transport.send(message); }
}
5.页面设置(denglu.jsp)
<form action="modPsw.action" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" id="userName2" name="userName2">
</td>
</tr>
<tr>
<td>邮箱:</td>
<td><input type="text" id="email1" name="email1"/></td>
</tr>
<tr>
<td colspan="2"><input type="button" value="返回">
<input type="submit" value="找回密码">
</td>
</tr>
</table>
</form>