转载来自:
我们经常遇到在网站或者软件注册新用户时需要向我们的注册邮箱发送一封激活邮件,然后我们去邮箱点击激活连接后我们的用户名才能登陆,其过程是当我们注册成功后数据库已经存入该用户的相关信息,但是用户状态为不可用,所以这时候该用户名是不能正常使用的。因此系统需要向我们的注册邮箱发一封激活邮件,我们点击激活连接后系统会将数据库中用户状态字段更改为可用状态,至此用户激活成功,该用户可以正常使用。下面是实现过程:
为了方便起见我们还是编写一个发送邮箱工具类。

[java] view plain copy 
package cn.itcast.shop.utils; import java.util.Properties; 
import javax.mail.Authenticator; 
 import javax.mail.Message; 
 import javax.mail.Message.RecipientType; 
 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; /** 
 * 邮件发送工具类 
 * @author shx 
 * 
 */ 
 public class MailUitls { 
 /** 
 * 发送邮件的方法 
 * @param to :收件人 
 * @param code :激活码 
 */ 
 public static void sendMail(String to,String code){ 
 /** 
 * 1.获得一个Session对象. 
 * 2.创建一个代表邮件的对象Message. 
 * 3.发送邮件Transport 
 */ 
 // 1.获得连接对象 
 Properties props = new Properties(); 
 props.setProperty(“mail.host”, “localhost”); 
 Session session = Session.getInstance(props, new Authenticator() {

@Override  
        protected PasswordAuthentication getPasswordAuthentication() {  
            return new PasswordAuthentication("service@shop.com", "111");  
        }  

    });  
    // 2.创建邮件对象:  
    Message message = new MimeMessage(session);  
    // 设置发件人:  
    try {  
        message.setFrom(new InternetAddress("service@shop.com"));  
        // 设置收件人:  
        message.addRecipient(RecipientType.TO, new InternetAddress(to));  
        // 抄送 CC   密送BCC  
        // 设置标题  
        message.setSubject("来自官方激活邮件");  
        // 设置邮件正文:  
        message.setContent("<h1>官方激活邮件!点下面链接完成激活操作!</h1><h3><a href='http://192.168.24.162:8080/shop/user_active.action?code="+code+"'>http://192.168.24.162:8080/shop/user_active.action?code="+code+"</a></h3>", "text/html;charset=UTF-8");  
        // 3.发送邮件:  
        Transport.send(message);  
    } catch (AddressException e) {  
        e.printStackTrace();  
    } catch (MessagingException e) {  
        e.printStackTrace();  
    }  

}  

public static void main(String[] args) {  
    sendMail("aaa@shop.com","11111111111111");  
}
} 

 Action: 
 [java] view plain copy 
 /** 
 * 
 * 用户注册 
 */ 
 public String regist(){ 
 userService.save(user); 
 this.addActionMessage(“注册成功,情趣邮箱激活!”); 
 return “msg”; 
 }

/** 
 * 用户激活的方法 
 */  
public String active() {  
    // 根据激活码查询用户:  
    User existUser = userService.findByCode(user.getCode());  
    // 判断  
    if (existUser == null) {  
        // 激活码错误的  
        this.addActionMessage("激活失败:激活码错误!");  
    } else {  
        // 激活成功  
        // 修改用户的状态  
        existUser.setState(1);  
        existUser.setCode(null);  
        userService.update(existUser);  
        this.addActionMessage("激活成功:请去登录!");  
    }  
    return "msg";  
}</span>

Service:

[java] view plain copy 
 // 业务层完成用户注册代码: 
 public void save(User user) { 
 // 将数据存入到数据库 
 user.setState(0); // 0:代表用户未激活. 1:代表用户已经激活. 
 String code = UUIDUtils.getUUID()+UUIDUtils.getUUID();//调用随机ID生成工具 
 user.setCode(code); 
 userDao.save(user); 
 // 发送激活邮件; 
 MailUitls.sendMail(user.getEmail(), code); 
 }

// 业务层根据激活码查询用户  
public User findByCode(String code) {  
    return userDao.findByCode(code);  
}  

// 修改用户的状态的方法  
public void update(User existUser) {  
    userDao.update(existUser);  
}</span>

Dao:

[java] view plain copy 
 public void save(User user) { 
 // TODO Auto-generated method stub 
 this.getHibernateTemplate().save(user); 
 }

// 根据激活码查询用户  
public User findByCode(String code) {  
    String hql = "from User where code = ?";  
    List<User> list = this.getHibernateTemplate().find(hql,code);  
    if(list != null && list.size() > 0){  
        return list.get(0);  
    }  
    return null;  
}  

// 修改用户状态的方法  
public void update(User existUser) {  
    this.getHibernateTemplate().update(existUser);  
}</span>

注册成功后激活之前数据库截图

激活之后数据库截图

因为我们只希望用户激活一次,所以激活成功后将数据的code字段清空,state改为1。目前系统没有上线,为了方便测试,我用的本地邮箱测试,仅支持局域网,需要外网的请参见博客,以上内容有不足之处请大家批评指正,谢谢!