刚入职 求指点 SPRING MVC3.0 发送邮件 修改密码
小弟最近刚刚入职,公司要求按照思路往下写,可刚刚接触这一块,一点没思路,望大神指点一二。需求就是按照这个写法写一个密码找回功能,发送到邮箱的,其中userName即为用户邮箱(邮箱作为用户名登录的)代码如下.下面怎么写呢,用来发邮件的工具类是有了,那么那些参数应该在哪里设置呢,比如邮件服务器地址什么的,实在不明白,请大神指点。
AdminUserCotroller类:
@Controller
@RequestMapping("/admin/user")
public class AdminUserController extends BaseController {
@Autowired
private IUserService userService;
/**
* 前往列表页面
* @return
*/
@RequestMapping
public String main() {
return "user/main";
}
/**
* 登录
* @param session
* @return
*/
@RequestMapping(value = "login" ,method = RequestMethod.GET)
public String login(HttpSession session){
User user = UserUtil.getUser(session) ;
return "user/main" ;
}
@RequestMapping(value = "/login", method = RequestMethod.POST)
@ResponseBody
public Map<String,String> login (String userName,String userPass,String yzm,HttpSession session){
if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(userPass) || StringUtils.isEmpty(yzm)){
retMap.put("success", "false");
retMap.put("message", "请输入完整信息");
return retMap;
}
if(!yzm.equals(session.getAttribute("code"))){
retMap.put("success", "false");
retMap.put("message", "验证码不正确");
return retMap;
}
User user = userService.getUniqueEntity("userName",userName);
if(user==null){
retMap.put("success", "false");
retMap.put("message", "用户不存在");
} else {
if(!MD5.MD5(userPass).equals(user.getUserName())){
retMap.put("success", "false");
retMap.put("message", "密码错误");
} else {
session.setAttribute("user",user);
}
}
return retMap;
}
/**
* 密码找回
*/
@RequestMapping(value = "findPass" , method = RequestMethod.GET)
public String findPass(){
return "user/findPass";
}
@RequestMapping(value = "findPass" , method = RequestMethod.GET)
public Map<String,String> findPass(String mailServerHost,
String mailServerPort, boolean validate, String fromAddress,
String userPass, String userName, String subject, String content,
boolean isHTML, boolean isSSL){
if(StringUtils.isEmpty(userName)){
retMap.put("success", "false");
retMap.put("message", "请输入邮箱信息");
return retMap;
}
User user = userService.getUniqueEntity("userName",userName);
if(user==null){
retMap.put("success", "false");
retMap.put("message", "用户不存在");
}else {
if(MailUtil.checkMail(userName)){
retMap.put("success", "false");
retMap.put("message", "邮箱格式不正确");
} else {
}
}
return retMap;
}
public static boolean sendMail(String mailServerHost,
String mailServerPort, boolean validate, String fromAddress,
String userPass, String toAddress, String subject, String content,
boolean isHTML, boolean isSSL) {
Properties p = new Properties();
p.put("mail.smtp.host", mailServerHost);
p.put("mail.smtp.port", mailServerPort);
p.put("mail.smtp.auth", validate ? "true" : "false");
if (isSSL) {
p.put("mail.smtp.starttls.enable", "true");
p.put("mail.smtp.socketFactory.fallback", "false");
p.put("mail.smtp.socketFactory.port", mailServerPort);
}
Authenticator auth = null;
if (validate) {
auth = new myAuthenticator(fromAddress, userPass);
}
try {
Session session = Session.getDefaultInstance(p, auth);
Message message = new MimeMessage(session);
Address from = new InternetAddress(fromAddress);
Address to = new InternetAddress(toAddress);
message.setFrom(from);
message.setRecipient(Message.RecipientType.TO, to);
message.setSubject(subject);
message.setSentDate(new Date());
if (isHTML) {
Multipart m = new MimeMultipart();
BodyPart bp = new MimeBodyPart();
bp.setContent(content, "text/html; charset=utf-8");
m.addBodyPart(bp);
message.setContent(m);
} else
message.setText(content);
Transport.send(message);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public static boolean checkMail(String email) {
boolean tag = true;
final String pattern1 = "w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*";
final Pattern pattern = Pattern.compile(pattern1);
final Matcher mat = pattern.matcher(email);
if (!mat.find()) {
tag = false;
}
return tag;
}
}
class myAuthenticator extends Authenticator {
String userName;
String userPass;
public myAuthenticator() {
}
public myAuthenticator(String userName, String userPass) {
this.userName = userName;
this.userPass = userPass;
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, userPass);
}
}
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
/****************************************************************
* 对QQ邮箱使用(对于其他的使用类似的你应该也会更改了)
* 首先把QQ邮箱的POP3 SMTP打开
* 首先确定你的网络正常、非代理
* 确定你的QQ邮箱开启了SMTP
* 如果没有开启,那么这样设置下
* 设置->账户 --在下面-- 把这个选上 [√]SMTP发信后保存到服务器
****************************************************************/
public class SendEmail {
//设置服务器
private static String KEY_SMTP = "mail.smtp.host";
private static String VALUE_SMTP = "smtp.qq.com";
//服务器验证
private static String KEY_PROPS = "mail.smtp.auth";
private static boolean VALUE_PROPS = true;
//发件人用户名、密码
private String SEND_USER = "184172133@qq.com";//你的QQ邮箱
private String SEND_UNAME = "184172133"; //你的邮箱名
private String SEND_PWD = "*********"; //你的邮箱密码
//建立会话
private MimeMessage message;
private Session s;
/*
* 初始化方法
* */
public SendEmail(){
Properties props = System.getProperties();
props.setProperty(KEY_SMTP, VALUE_SMTP);
props.put(KEY_PROPS, VALUE_PROPS);
s = Session.getInstance(props);
/*s.setDebug(true);开启后有调试信息*/
message = new MimeMessage(s);
}
/**
* 发送邮件
* @param headName 邮件头文件名
* @param sendHtml 邮件内容
* @param receiveUser 收件人184172133@qq.com
*/
public void doSendHtmlEmail(String headName,StringBuffer sendHtml,String receiveUser){
try {
// 发件人
InternetAddress from = new InternetAddress(SEND_USER);
message.setFrom(from);
// 收件人
InternetAddress to = new InternetAddress(receiveUser);
message.setRecipient(Message.RecipientType.TO, to);
// 邮件标题
message.setSubject(headName);
String content = sendHtml.toString();
// 邮件内容,也可以使纯文本"text/plain"
message.setContent(content, "text/html;charset=GBK");
message.saveChanges();
Transport transport = s.getTransport("smtp");
// smtp验证,就是你用来发邮件的邮箱用户名密码
transport.connect(VALUE_SMTP, SEND_UNAME, SEND_PWD);
// 发送
transport.sendMessage(message, message.getAllRecipients());
transport.close();
} catch (AddressException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
}
}