MVC模式下向qq邮箱发送邮件
将已经保存在数据库中的密码通过邮件发送到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>
MVC模式下向qq邮箱发送邮件的更多相关文章
- MongoDB学习笔记(三) 在MVC模式下通过Jqgrid表格操作MongoDB数据
看到下图,是通过Jqgrid实现表格数据的基本增删查改的操作.表格数据增删改是一般企业应用系统开发的常见功能,不过不同的是这个表格数据来源是非关系型的数据库MongoDB.nosql虽然概念新颖,但是 ...
- ecshop QQ邮箱发送邮件服务器配置
ecshop QQ邮箱发送邮件服务器配置 1.邮件服务:采用其他的SMTP服务 2.邮件服务器是否要求加密连接(SSL): 是 此项设置需要php支持openSSL模块 开启方法: a.php.ini ...
- python3通过qq邮箱发送邮件
python3通过qq邮箱发送邮件 0.了解qq邮箱的SMTP QQ邮箱 POP3 和 SMTP 服务器地址设置如下: 邮箱 POP3服务器(端口995) SMTP服务器(端口465或587) qq. ...
- 基于java mail实现简单的QQ邮箱发送邮件
刚学习到java邮件相关的知识,先写下这篇博客,方便以后翻阅学习. -----------------------------第一步 开启SMTP服务 在 QQ 邮箱里的 设置->账户里开启 S ...
- legend3---lavarel中使用qq邮箱发送邮件
legend3---lavarel中使用qq邮箱发送邮件 一.总结 一句话总结: 第一步:配置邮箱做服务器,比如qq邮箱,网易163邮箱 第二步:配置lavarel的配置文件 第三部:写邮件发送代码就 ...
- 在MVC模式下通过Jqgrid表格操作MongoDB数据
看到下图,是通过Jqgrid实现表格数据的基本增删查改的操作.表格数据增删改是一般企业应用系统开发的常见功能,不过不同的是这个表格数据来源是非关系型的数据库MongoDB.nosql虽然概念新颖,但是 ...
- 杂项之使用qq邮箱发送邮件
杂项之使用qq邮箱发送邮件 本节内容 特殊设置 测试代码 1. 特殊设置 之前QQ邮箱直接可以通过smtp协议发送邮件,不需要进行一些特殊的设置,但是最近使用QQ邮箱测试的时候发现以前使用的办法无法奏 ...
- MVC模式下My97DatePicker日期控件引用注意事项
My97DatePicker日期控件之前在用webform模式开发的时候,只要 <script language="javascript" type="text/j ...
- SpringBoot使用qq邮箱发送邮件
最近公司要做一个邮箱注册和重置密码的功能,因为之前就做过,但是不是Springboot项目,所以相对来说还是比较容易的,在这里记录一下. 一.引用Maven依赖 这里使用spring自带的邮件jar包 ...
随机推荐
- Jmeter使用入门
修改时间 修改内容 修改人 2016.3.12 创建 刘永志 2016.6.18 完成 刘永志 Jmeter简介 Jmeter的基本概念 百度百科: Apache JMeter是Apache组织开发的 ...
- react-组件生命周期
本文同步至微信公众号http://mp.weixin.qq.com/s?__biz=MzAxMzgwNDU3Mg==&mid=402267570&idx=1&sn=4b0dc2 ...
- ArcGIS Engine 刷新问题
link: http://www.cnblogs.com/Jingkunliu/archive/2013/01/10/2854710.html PartialRefresh方法是部分刷新,效率方面比单 ...
- SQL SELECT SET
SELECT SET 同时对多个变量同时赋值时 支持 不支持 表达式返回多个值时 将返回的最后一个值赋给变量 出错 表达式未返回值时 变量保持原值 变量被赋null值
- 查看表的datapages
select object_name(i.object_id) as tableName,data_pages as dataPagesfrom sys.indexes as ijoin sys.pa ...
- python安装tkinter
python2安装tkinter sudo apt-get install python-tk python3 安装tkinter sudo apt-get install python3-tk
- 在CentOS上搭建Storm集群
Here's a summary of the steps for setting up a Storm cluster: Set up a Zookeeper clusterInstall depe ...
- 【Python】函数基础简介
一.函数 1. 简介 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段.函数能提高应用的模块性,和代码的重复利用率. 2. 组成 函数代码块以 def 关键词开头,后接函数名和圆括号( ...
- 编码UTF-8
☯,首先,这并不是图片,这是一个unicode字符,Yin Yang,即阴阳符,码点为U+262F.如果你的浏览器无法显示,可以查看这个链接http://www.fileformat.info/inf ...
- python打印服务器所有进程
#有时候我们需要查看服务器上所有进程,来判断哪些进程是否已经称为僵尸进程#!/usr/local/bin/python3.5 import psutil for i in psutil.pids(): ...