所需jar:activation-1.1.1,commons-email-1.3.2,mail-1.4.7。
/**
*
*/
package com.controller;
import java.util.Random;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.util.sendEmailUtil;
/**
*
* @ClassName: EmailCodeController
* @Description: 发送邮箱验证码
* @author Hacker_枫
* @date 2016年7月25日 上午10:08:21
*
*/
@Controller
@RequestMapping("/admin/email")
public class EmailCodeController extends BaseController{
@RequestMapping(value = "/emailCode", method=RequestMethod.GET)
public String emailCode(){
return "/admin/email/send";
}
/**
* 发送邮箱验证码
* @Title: sendEmailCode
* @param request
* @param response
* @param email
* @return
* @author Hacker_枫
* @since 2016年7月26日 V 1.0
*/
@RequestMapping(value = "/sendEmailCode", method = RequestMethod.POST)
@ResponseBody
public String sendEmailCode(HttpServletRequest request, HttpServletResponse response, String email){
//随机生成一个最大为999999的整数
int number = new Random().nextInt(999999);
//将生成数转换为字符串,不足6为左侧用0补全
String code = String.format("%06d", number);
//发送邮件的主题
String subject = "邮箱验证码";
//验证码的内容
String msg = "您的邮箱验证码是:" + code + ",有效期为三分钟,如非本人操作请忽略。本消息由张先生通过java代码编制而出,如有需要,请联系:18300247868!";
//发送邮件
boolean isOk=sendEmailUtil.sendEmail(email, msg ,subject);
if(isOk){
//保存在cookie
Cookie cookie = new Cookie("emailCode", code);
//设置cookie最大有效时间为180秒
cookie.setMaxAge(180);
//将cookie放在response里
response.addCookie(cookie);
return message(Type.success, "发送验证码成功,请登录邮箱查收!");
}
return message(Type.error, "发送验证码失败,请重试!");
}
/**
* 邮箱验证
* @Title: emailInsert
* @param request
* @param response
* @param email 邮箱
* @param emailCode 验证码
* @return
* @author Hacker_枫
* @since 2016年7月26日 V 1.0
*/
@RequestMapping(value = "saveEmail", method = RequestMethod.POST)
@ResponseBody
public String emailInsert(HttpServletRequest request,HttpServletResponse response, String email, String emailCode){
//定义变量
String code="";
//获取cookie中存放的值
Cookie [] cookie = request.getCookies();
//判断
if(StringUtils.isEmpty(email)){
return message(Type.error, "邮箱不能为空");
}else if(cookie.length<1){
return message(Type.error, "请先发送验证码");
}else if(StringUtils.isEmpty(cookie)){
return message(Type.error, "请填写验证码");
}
for(int i=0;i<% StringBuffer url = request.getRequestURL();
String base = url.delete(url.length() - request.getRequestURI().length(), url.length()).append(request.getServletContext().getContextPath()).toString();
request.setAttribute("base", base);
%>
发送邮箱验证码
<script type="text/javascript" src="${base}/resource/js/jquery-1.6.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
//发送邮箱验证码
$("#sendEmailCode").click(function(){
if($("#email").val() != null && $("#email").val() != ""){
$.ajax({
type:"post",
dataType:"json",
url:"${base}/admin/email/sendEmailCode.do",
data:{email:$("#email").val()},
success:function(message){
if(message.type == "success"){
alert(message.content);
}else{
alert(message.content);
}
},
error:function(){
alert("系统繁忙,请稍后再试。");
}
});
}else{
alert("请填写您的邮箱地址");
}
});
$("#email_submit").click(function(){
$.ajax({
type:"post",
dataType:"json",
data:{
email:$("#email").val(),
emailCode:$("#emailCode").val()
},
url:"${base}/admin/email/saveEmail.do",
success:function(message){
if(message.type == "success"){
alert(message.content);
}else{
alert(message.content);
}
},
error:function(){
alert("系统繁忙,请稍后重试");
}
});
});
});
</script>
empty/**
* @sendEmail.java
* @com.util
* @Description:
*
* @author Hacker_枫
* @copyright 2016
* @version V
* @since 2016年7月25日
*/
package com.util;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
/**
* @ClassName: sendEmail
* @Description: 发送邮件
* @author Hacker_枫
* @date 2016年7月25日 上午11:52:45
*/
public class sendEmailUtil {
/**
* 发送邮件
* @Title: sendEmail
* @param receiverEmail 接收人邮箱
* @param msg 发送的消息内容
* @return
* @author Hacker_枫
* @since 2016年7月26日 V 1.0
*/
public static boolean sendEmail(String receiverEmail, String msg, String subject){
//实例化一个html邮件
HtmlEmail email = new HtmlEmail();
try {
//设置为qq邮箱发送
email.setHostName("smtp.qq.com");
email.setSSL(true);
//设置SSL端口号为465(固定)
email.setSslSmtpPort("465");
//设置编码
email.setCharset("UTF-8");
//接收人邮箱
email.addTo(receiverEmail);
//发送人邮箱,昵称
email.setFrom("邮箱地址","昵称");
//设置发送人邮箱,及smtp密码
email.setAuthentication("邮箱地址", "smtp密码");
//邮件主题
email.setSubject(subject);
//邮件内容
email.setMsg(msg);
//发送邮件
email.send();
return true;
} catch (EmailException e) {
e.printStackTrace();
return false;
}
}
}