背景:

易邮邮件服务器软件,foxmail客户端软件,在本地服务器上注册两个邮箱账号进行测试。原理就是用户通过注册向数据库插入一条数据,数据库字段有激活状态(1,0表示),64位随机生成的验证码。然后再向填写的邮件发送一封激活邮件,点击链接,改变激活状态并更新。环境是由ssh框架下搭建的,下面贴代码

数据库字段

(由hibernater自动生成表结构)

CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(255) DEFAULT NULL,
  `userpass` varchar(255) DEFAULT NULL,
  `nickname` varchar(255) DEFAULT NULL,
  `email` varchar(255) DEFAULT NULL,
  `status` int(11) DEFAULT NULL,
  `code` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=27 DEFAULT CHARSET=utf8;

注册jsp页面

<form action="user/user_save" method="post">
        <s:textfield name="user.username" label="账户"/><br>
        <s:textfield name="user.userpass" label="密码"/><br>
        <s:textfield name="user.nickname" label="昵称"/><br>
        <s:textfield name="user.email" label="邮箱"/><br>
        <input type="submit" value="提交">
</form>

action部分

private User user;

        private UserService userService;

        public String save() throws Exception {
                user.setCode(tool.getUUID());
                System.out.println(user);
                userService.save(user);
                mailtool.sendMail(user.getEmail(), user.getCode());

        return "ok";
        }
        public String active(){
            String code = ServletActionContext.getRequest().getParameter("code");
            User findUser = userService.findUser(code);
            if(findUser!=null){
                findUser.setCode(null);
                findUser.setStatus(1);
                userService.update(findUser);
            }
            return SUCCESS;
        }



        public User getUser() {
            return user;
        }

        public void setUser(User user) {
            this.user = user;
        }

        public UserService getUserService() {
            return userService;
        }

        public void setUserService(UserService userService) {
            this.userService = userService;
        }

User类

        private int id;
        private String username;
        private String userpass;
        private String nickname;
        private String email;
        private int status;
        private String code;

UUIDtool

生成64位验证码

public static String getUUID(){
            return UUID.randomUUID().toString().toString().replace("-", "")+UUID.randomUUID().toString().toString().replace("-", "");
        }

mailtool发送邮件(引相关jar包)

public static void sendMail(String to,String code){
        //创建链接对象 链接服务器
            Properties props=new Properties();
            Session session=Session.getInstance(props, new Authenticator(){
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    // TODO Auto-generated method stub
                    return new PasswordAuthentication("user1@hehe.com", "123");
                }
            });

        //创建邮件对象
            Message message=new MimeMessage(session);
            try {
                message.setFrom(new InternetAddress("user1@hehe.com"));
                message.setRecipient(RecipientType.TO, new InternetAddress(to));
                message.setSubject("来自xx的激活邮件");
                message.setContent("<h1>来自xx网站的激活邮件,请点击以下链接:</h1><a href='http://localhost:8080/MailDemo/user/user_active?code="+code+"'>"+code+"</a>","text/html;charset=utf-8");
                //发送邮件
                Transport.send(message);
                System.out.println("<h1>来自xx网站的激活邮件,请点击以下链接:</h1><a href='http://localhost:8080/MailDemo/user/user_active?code="+code+"'></a>");
            } catch (AddressException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (MessagingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

dao层

public User findUser(String code){
        String sql="from User u where u.code=?";
         List<User> query = this.getHibernateTemplate().find(sql,code);
         return query.get(0);
}

    public void update(User user) {
            getHibernateTemplate().update(user);

    }
    public void save(User user){
             this.getHibernateTemplate().save(user);
    }