登录模块
用户忘记密码,获取验证码
@RequestMapping(value = "/forApp/denglu.jspx")
public void getNewregister(HttpServletRequest request,
HttpServletResponse response, ModelMap model) throws Exception {
response.addHeader("Access-Control-Allow-Origin", "*");
response.setContentType("text/plain;charset=UTF-8");
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
PrintWriter out = response.getWriter();
//输入注册信息
String email = request.getParameter("email");
//判断是否为空,可根据项目自身进行判断
if (!BaseAppUtils.isEmptyAfterTrim(email)
&& !BaseAppUtils.isEmptyAfterTrim(email)) {
//获取验证码
randomSecurity = EmailUtils.sendApps(email,request);
if(randomSecurity!=null){
//out.println(BaseAppUtils.SUCCESS());
BaseAppUtils.responseStr(response, "成功");
out.flush();
out.close();
}else{
//out.println(BaseAppUtils.FAIL_ORC("验证码发送失败"));
BaseAppUtils.responseStrError(response, "验证码发送失败");
out.flush();
out.close();
}
}else{
//out.println(BaseAppUtils.FAIL_ORC("邮箱为空"));
BaseAppUtils.responseStrError(response, "邮箱为空");
out.flush();
out.close();
}
}
public static Map<String,String> sendApps(String email, HttpServletRequest request)
{
int random=((int)(Math.random()*9000+1000));
try {
// 将旧的验证码移除
randomSecurity.remove(email);
//request.getSession().setAttribute(appToken, String.valueOf(random));
// 向map中放入新的验证码
randomSecurity.put(email, String.valueOf(random));
// 发件人的账号
String userName = "x'x'xxxx.com";
// 账号密码
String password = "xxxx";
//阿里云邮箱服务地址
String smtp_server = "smtp.mxhichina.com";
//发件人
String from_mail_address = userName;
//收件人
String to_mail_address = email;
// 构建授权信息,用于进行SMTP进行身份验证
Authenticator auth = new PopupAuthenticator(userName, password);
// 配置发送邮件的环境属性
Properties mailProps = new Properties();
//账号密码
mailProps.put("mail.user", userName);
mailProps.put("mail.password", password);
// 表示SMTP发送邮件,需要进行身份验证
mailProps.put("mail.smtp.host", smtp_server);
mailProps.put("mail.smtp.port", "25");
mailProps.put("mail.smtp.auth", "true");
// 使用环境属性和授权信息,创建邮件会话
Session mailSession = Session.getDefaultInstance(mailProps, auth);
mailSession.setDebug(true);
//邮件昵称
String nick=javax.mail.internet.MimeUtility.encodeText("xxx");
// 创建邮件消息
MimeMessage message = new MimeMessage(mailSession);
// 设置发件人
message.setFrom(new InternetAddress(nick+" <"+from_mail_address+">"));
// 设置收件人
message.setRecipient(Message.RecipientType.TO, new InternetAddress(
to_mail_address));
// 设置邮件标题
message.setSubject("xxx");
MimeMultipart multi = new MimeMultipart();
BodyPart textBodyPart = new MimeBodyPart();
//发送邮件内容
textBodyPart.setContent("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"
+ "<html><head><title></title><style type=\"text/css\">body{font-size:14px;font-family:verdana;line-"
+ "height:30px;}</style></head><body>您好:您的旅日侨网邮箱验证码为:<br />"+random+"<br />"
+ "<br />"
+ "</a><br />此邮件为"
+ "系统自动发送,请勿回复。</body></html>", "text/html;charset=utf-8");
multi.addBodyPart(textBodyPart);
message.setContent(multi);
message.saveChanges();
//发送
Transport.send(message);
} catch (Exception ex) {
System.err.println("邮件发送失败的原因是:" + ex.getMessage());
System.err.println("具体的错误原因");
ex.printStackTrace(System.err);
return null;
}
return randomSecurity;
}