Java——用户激活邮件工具类

x33g5p2x  于2021-03-13 发布在 其他  
字(3.4k)|赞(0)|评价(0)|浏览(404)

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

为了方便起见我们还是编写一个发送邮箱工具类。

  1. package cn.itcast.shop.utils;
  2. import java.util.Properties;
  3. import javax.mail.Authenticator;
  4. import javax.mail.Message;
  5. import javax.mail.Message.RecipientType;
  6. import javax.mail.MessagingException;
  7. import javax.mail.PasswordAuthentication;
  8. import javax.mail.Session;
  9. import javax.mail.Transport;
  10. import javax.mail.internet.AddressException;
  11. import javax.mail.internet.InternetAddress;
  12. import javax.mail.internet.MimeMessage;
  13. /**
  14. * 邮件发送工具类
  15. * @author shx
  16. *
  17. */
  18. public class MailUitls {
  19. /**
  20. * 发送邮件的方法
  21. * @param to :收件人
  22. * @param code :激活码
  23. */
  24. public static void sendMail(String to,String code){
  25. /**
  26. * 1.获得一个Session对象.
  27. * 2.创建一个代表邮件的对象Message.
  28. * 3.发送邮件Transport
  29. */
  30. // 1.获得连接对象
  31. Properties props = new Properties();
  32. props.setProperty("mail.host", "localhost");
  33. Session session = Session.getInstance(props, new Authenticator() {
  34. @Override
  35. protected PasswordAuthentication getPasswordAuthentication() {
  36. return new PasswordAuthentication("service@shop.com", "111");
  37. }
  38. });
  39. // 2.创建邮件对象:
  40. Message message = new MimeMessage(session);
  41. // 设置发件人:
  42. try {
  43. message.setFrom(new InternetAddress("service@shop.com"));
  44. // 设置收件人:
  45. message.addRecipient(RecipientType.TO, new InternetAddress(to));
  46. // 抄送 CC 密送BCC
  47. // 设置标题
  48. message.setSubject("来自官方激活邮件");
  49. // 设置邮件正文:
  50. 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");
  51. // 3.发送邮件:
  52. Transport.send(message);
  53. } catch (AddressException e) {
  54. e.printStackTrace();
  55. } catch (MessagingException e) {
  56. e.printStackTrace();
  57. }
  58. }
  59. public static void main(String[] args) {
  60. sendMail("aaa@shop.com","11111111111111");
  61. }
  62. }

Action:

  1. /**
  2. *
  3. * 用户注册
  4. */
  5. public String regist(){
  6. userService.save(user);
  7. this.addActionMessage("注册成功,情趣邮箱激活!");
  8. return "msg";
  9. }
  10. /**
  11. * 用户激活的方法
  12. */
  13. public String active() {
  14. // 根据激活码查询用户:
  15. User existUser = userService.findByCode(user.getCode());
  16. // 判断
  17. if (existUser == null) {
  18. // 激活码错误的
  19. this.addActionMessage("激活失败:激活码错误!");
  20. } else {
  21. // 激活成功
  22. // 修改用户的状态
  23. existUser.setState(1);
  24. existUser.setCode(null);
  25. userService.update(existUser);
  26. this.addActionMessage("激活成功:请去登录!");
  27. }
  28. return "msg";
  29. }

Service:

  1. // 业务层完成用户注册代码:
  2. public void save(User user) {
  3. // 将数据存入到数据库
  4. user.setState(0); // 0:代表用户未激活. 1:代表用户已经激活.
  5. String code = UUIDUtils.getUUID()+UUIDUtils.getUUID();//调用随机ID生成工具
  6. user.setCode(code);
  7. userDao.save(user);
  8. // 发送激活邮件;
  9. MailUitls.sendMail(user.getEmail(), code);
  10. }
  11. // 业务层根据激活码查询用户
  12. public User findByCode(String code) {
  13. return userDao.findByCode(code);
  14. }
  15. // 修改用户的状态的方法
  16. public void update(User existUser) {
  17. userDao.update(existUser);
  18. }

Dao:

  1. public void save(User user) {
  2. // TODO Auto-generated method stub
  3. this.getHibernateTemplate().save(user);
  4. }
  5. // 根据激活码查询用户
  6. public User findByCode(String code) {
  7. String hql = "from User where code = ?";
  8. List<User> list = this.getHibernateTemplate().find(hql,code);
  9. if(list != null && list.size() > 0){
  10. return list.get(0);
  11. }
  12. return null;
  13. }
  14. // 修改用户状态的方法
  15. public void update(User existUser) {
  16. this.getHibernateTemplate().update(existUser);
  17. }

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

激活之后数据库截图

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

相关文章