对于新手来说,qq邮箱短信验证是最佳选择啦,之前写了一个在本地测试ok但是到了服务器就不行了,今天就随便分享一下
前台js:
$("#sendidcode").click(function(){
if($("#mail").val() == ""){
$("#sendidcode").attr("disabled","true");
$("#sendidcode").val("邮箱地址未填");
setTimeout(function(){
$("#sendidcode").removeAttr("disabled");
$("#sendidcode").val("发送验证码");
},900);
return;
}
var myreg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
if(!myreg.test($("#mail").val())){
$("#sendidcode").attr("disabled","true");
$("#sendidcode").val("邮箱格式错误");
setTimeout(function(){
$("#sendidcode").removeAttr("disabled");
$("#sendidcode").val("发送验证码");
},900);
return;
}
$("#sendidcode").attr("value","正在发送");
$("#sendidcode").attr("disabled","true");
$.ajax({
url:"sendIdCode.action",
type:"GET",
//发送数据的第一种格式,字符串...
data:"mail="+$("#mail").val(),
timeout:5000,
success:function(data){
$("#sendidcode").attr("value","验证码已发送");
},
error:function(data){
$("#sendidcode").attr("value","验证码发送失败");
}
});
});
Controller:
/**
* 注册发送邮件
* @param mail
* @param response
* @param request
* @throws IOException
* @throws MessagingException
* @throws javax.mail.MessagingException
*/
@RequestMapping("/sendIdCode.action")
public void AjaxSendIdCode(String mail, HttpServletResponse response,
HttpServletRequest request) throws IOException, MessagingException,
javax.mail.MessagingException {
PrintWriter out = response.getWriter();
response.setCharacterEncoding("utf-8");
//随机产生6位随机数作为验证码
String code = "";
Random random = new Random();
for (int i = 0; i < 6; i++) {
int r = random.nextInt(10); //每次随机出一个数字(0-9)
code = code + r; //把每次随机出的数字拼在一起
}
StringBuilder builder = new StringBuilder();
builder.append("尊敬的" + mail + " 【先生/女士】:欢迎注册!您的验证码是:" + code
+ " ,仅用于本网站会员注册激活,请勿告知他人【毕业生就业管理系统】。");
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
builder.append("\n时间: " + dateFormat.format(new Date()));
MailUtil mailUtil = new MailUtil();
mailUtil.sendMail(mail, builder.toString());
request.getSession().setAttribute("idcode", code);
//request.setAttribute("idcode", code);
out.write("success");
}
MailUtil.java
package com.lgx.common.utils;
import java.util.Properties;
import javax.mail.Address;
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;
public class MailUtil {
static Properties properties;
static Message msg;
static Transport transport;
//初始化Mail信息
public MailUtil(){
/*// 网易163邮箱的 SMTP 服务器地址为: smtp.163.com 腾讯: smtp.qq.com
private String myEmailSMTPServer = "smtp.qq.com";*/
properties = new Properties();
properties.setProperty("mail.debug", "true");//调试模式发送
properties.setProperty("mail.smtp.auth", "true");//身份验证设置
properties.setProperty("mail.host", "smtp.qq.com");//发件人邮箱主机名
properties.setProperty("mail.transport.protocol", "smtp");//发件协议
final String smtpPort = "465";
properties.setProperty("mail.smtp.port", smtpPort);
properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
properties.setProperty("mail.smtp.socketFactory.fallback", "false");
properties.setProperty("mail.smtp.socketFactory.port", smtpPort);
Session session = Session.getInstance(properties);
msg = new MimeMessage(session);
try {
msg.setSubject("毕业生就业管理系统验证邮件");//邮箱主题
msg.setFrom(new InternetAddress("xxxx@qq.com"));//设置发件人
transport = session.getTransport();
transport.connect("xxxx@qq.com", "xxxx");//设置发件人在该邮箱主机上的用户名密码
} catch (MessagingException e) {
e.printStackTrace();
}
}
/**
* 得到邮箱地址邮箱内容发送。
* @param 邮箱地址
* @param 邮箱内容
* @throws AddressException
* @throws MessagingException
*/
public void sendMail(String address,String text) throws AddressException, MessagingException{
msg.setText(text);
transport.sendMessage(msg, new Address[] {new InternetAddress(address)});
transport.close();
}
}
还需要一个mail.jar 。OK! 时间有限就不多说了