基于163邮件服务器实现邮箱验证

基于163实现邮箱验证,需先开启邮箱的SMTP服务,利用javax中的mail包实现对邮件的操作。

MailUtil.java

public class MailUtil {
    public static final String FROM = "*******@163.com"; //发件人的email
    public static final String PWD = "*****"; //发件人密码--邮箱密码
    public static final String URL = "http://****"; //项目主页
    public static final int TIMELIMIT = 1000*60*60*24; //激活邮件过期时间24小时
    public static final String HOST = "smtp.163.com"; //163的smtp服务器
    public static final String SMTP = "smtp";
    //激活账号
    public static UserAuth activateMail(UserAuth user) throws Exception {
        //注册邮箱
        String email  = user.getAccount();
        //当前时间戳
        Long curTime = System.currentTimeMillis();
        //激活的有效时间
        Long activateTime = curTime+TIMELIMIT;
        //激活码--用于激活邮箱账号
        String token = Md5Util.md5(email+curTime);
        user.setToken(token);
        //过期时间
        user.setTimes(activateTime);
        //发送的邮箱内容
        String content = "

欢迎使用MINISCENE!

请在24小时内点击下面的链接激活帐户:"+"
"+URL+"/user/activate.html?v=1.0&token="+token+"&email="+email+"

"; //调用发送邮箱服务 MailUtil.sendMail(email, "MINISCENE账号激活", content); return user; }     //找回密码 public static int codeMail(String email,String code){ //发送的邮箱内容 String content = "

答应我,不要轻易告诉别人验证码,并在5分钟内重置密码:
请收好您的验证码:"+code+"

"; //调用发送邮箱服务 try{ MailUtil.sendMail(email, "密码找回", content); return 1; }catch (Exception e){ e.printStackTrace(); return 0; } }     //邮件发送 public static void sendMail(String email,String title,String content) throws Exception { Properties props = new Properties(); //加载一个配置文件 // 使用smtp:简单邮件传输协议 props.put("mail.smtp.host", HOST);//存储发送邮件服务器的信息 props.put("mail.smtp.port", 465);//设置端口 props.put("mail.smtp.auth", "true");//同时通过验证 props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); Session session = Session.getInstance(props);//根据属性新建一个邮件会话 MimeMessage message = new MimeMessage(session);//由邮件会话新建一个消息对象 message.setFrom(new InternetAddress(FROM));//设置发件人的地址 message.setRecipient(Message.RecipientType.CC, new InternetAddress(FROM)); //抄送 message.setRecipient(Message.RecipientType.TO, new InternetAddress(email));//设置收件人,并设置其接收类型为TO message.setSubject(title);//设置标题 //设置信件内容 message.setContent(content, "text/html;charset=gbk"); //发送HTML邮件 message.setSentDate(new Date());//设置发信时间 message.saveChanges();//存储邮件信息 //发送邮件 Transport transport = session.getTransport(SMTP); transport.connect(FROM, PWD); transport.sendMessage(message, message.getAllRecipients());//发送邮件,其中第二个参数是所有已设好的收件人地址 transport.close(); } }

生成token的MD5加密工具:

MD5Util.java

public class Md5Util {
    //MD5加密,生成32位md5码
    public static String md5(String inStr){
        MessageDigest StrMD5 = null;
        try{
            StrMD5 = MessageDigest.getInstance("MD5");
        }catch (Exception e){
            e.printStackTrace();  
            return "";  
        }  
        char[] charArray = inStr.toCharArray();  
        byte[] byteArray = new byte[charArray.length];  
        for (int i = 0; i < charArray.length; i++)
            byteArray[i] = (byte) charArray[i];
        byte[] md5Bytes = StrMD5.digest(byteArray);
        StringBuffer hexValue = new StringBuffer();  
        for (int i = 0; i < md5Bytes.length; i++){
            int val = ((int) md5Bytes[i]) & 0xff;
            if (val < 16)
                hexValue.append("0");
            hexValue.append(Integer.toHexString(val));
        }  
        return hexValue.toString();  
    }
}

你可能感兴趣的:(框架知识)