Java实现QQ邮箱验证

对于新手来说,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! 时间有限就不多说了

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值