欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java注册帐号邮箱激活验证实现

程序员文章站 2024-01-07 21:54:22
...

Java注册帐号邮箱激活验证实现

1.需要加入发送邮件的mail.jar:

http://www.oracle.com/technetwork/java/javamail/index-138643.html

 

2.将字符串通过MD5进行加密的工具类:

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * 创建人: zy
 * 创建时间: 2014年10月22日 下午2:36:19
 * 类描述:将字符串进行加密的工具类
 */
public class MD5Util {

	/**
	 * 将源字符串通过MD5进行加密为字节数组
	 * @param source
	 * @return
	 */
	public static byte[] encodeToBytes(String source) {
		byte[] result  = null;
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.reset();//重置
			md.update(source.getBytes("UTF-8"));//添加需要加密的源
			result = md.digest();//加密
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 将源字符串通过MD5加密成32位16进制数
	 * @param source
	 * @return
	 */
	public static String encodeToHex(String source) {
		byte[] data = encodeToBytes(source);//先加密为字节数组
		StringBuffer hexSb = new StringBuffer();
		for (int i = 0; i < data.length; i++) {
			String hex = Integer.toHexString(0xff & data[i]);
			if (hex.length() == 1) {
				hexSb.append("0");
			}
			hexSb.append(hex);
		}
		return hexSb.toString();
	}
	
	/**
	 * 验证字符串是否匹配
	 * @param unknown	待验证的字符串
	 * @param okHex		使用MD5加密后的16进制字符串
	 * @return
	 */
	public static boolean validate(String unknown , String okHex) {
		return okHex.equals(encodeToHex(unknown));
	}
}

 

 

3.发送邮件的工具类:

import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 * 创建人: zy
 * 创建时间: 2014年10月22日 下午3:04:11
 * 类描述:发送邮件工具类
 */
public class SendMailUtil {
	
	public static final String HOST = "smtp.163.com";
	public static final String PROTOCOL = "smtp";
	public static final int PORT = 25;
	public static final String SENDER = "***@163.com";//此处需要与host相同的网站
	public static final String SENDERPWD = "********";
	
	/**
	 * 获取用于发送邮件的Session
	 * @return
	 */
	public static Session getSession() {
		Properties props = new Properties();
		props.put("mail.smtp.host", HOST);//设置服务器地址
        props.put("mail.store.protocol" , PROTOCOL);//设置协议
        props.put("mail.smtp.port", PORT);//设置端口
        props.put("mail.smtp.auth" , true);
        
        Authenticator authenticator = new Authenticator() {
        	@Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(SENDER, SENDERPWD);
            }
		};
        Session session = Session.getDefaultInstance(props,authenticator);
        return session;
	}
	
	/**
	 * 发送邮件
	 * @param receiver
	 * @param content
	 */
	public static void send(String receiver, String content) {
		Session session = getSession();
		try {
			System.out.println("-------开始发送-------");
			Message msg = new MimeMessage(session);
			//设置message属性
			msg.setFrom(new InternetAddress(SENDER));
			InternetAddress[] addrs = {new InternetAddress(receiver)};
			msg.setRecipients(Message.RecipientType.TO, addrs);
			msg.setSubject("开心网-帐号激活");
			msg.setSentDate(new Date());
			msg.setContent(content,"text/html;charset=utf-8");
			//开始发送
			Transport.send(msg);
                        System.out.println("-------发送完成-------");
		} catch (AddressException e) {
			e.printStackTrace();
		} catch (MessagingException e) {
			e.printStackTrace();
		}
	}
}

 

 4.发送邮件的Servlet或Action:

User user = userDao.getUser();
if (user != null && user.getQybj() != 1) {
	String email = user.getEmail();
  	StringBuffer content = new StringBuffer("<h2>请点击下面的链接激活帐号,链接只能使用一次,请尽快激活!</h2>");
  	String nameMd5 = MD5Util.encodeToHex(user.getUsername()+"审核通过");//创建加密字符串
  	content.append("<a style='font-size:16px;' href='http://localhost:8080/czfc/register.reg?doType=activate&")
  	.append("email=" + email + "&name=" + nameMd5 +"'>")
  	.append("http://localhost:8080/czfc/register.reg?doType=activate&")
  	.append("email=" + email + "&name=" + nameMd5 + "</a><br/><br/>")
  	.append("<span style='color:blue;font-size:20px;font-weight:bold;'>———浙江常山欢迎您!<span>");
  	SendMailUtil.send(email, content.toString());//开始发送邮件
} 

 

5.点击链接验证的Servlet或Action:

String userName = request.getParameter("name");
String email = request.getParameter("email");
UserPostDao userDao = new UserPostDao();
User user = userDao.getUserByEmail(email);
if (user == null) {
	destination = "/register/activeFail.jsp";
	request.setAttribute("msg", "链接错误");
}
else {
	String userNameMd5 = MD5Util.encodeToHex(user.getUsername()+"审核通过");
	if (!(userName.equals(userNameMd5)) {
		request.setAttribute("msg", "链接错误");
		destination = "/register/activeFail.jsp";
	}
	else {
		boolean done = userDao.updateUserStatusByEmail(email, 1);
		if (done) {
			request.setAttribute("userId", user.getUserid());
			request.setAttribute("userName", user.getUsername());
			destination = "/register/activeSuccess.jsp";
		}
		else {
			request.setAttribute("msg", "激活出错");
			destination = "/register/activeFail.jsp";
		}
	}
}

 


Java注册帐号邮箱激活验证实现
            
    
    博客分类: Java Javaemail 
 
Java注册帐号邮箱激活验证实现
            
    
    博客分类: Java Javaemail 
 

  • Java注册帐号邮箱激活验证实现
            
    
    博客分类: Java Javaemail 
  • 大小: 18.1 KB
  • Java注册帐号邮箱激活验证实现
            
    
    博客分类: Java Javaemail 
  • 大小: 17.3 KB
相关标签: Java email

上一篇:

下一篇: