使用JavaMail实现激活示例

本文介绍如何使用JavaMail在Java应用中实现用户注册后的邮箱激活功能,包括配置邮件发送参数、构造邮件内容及激活链接、发送邮件以及通过激活码更新用户状态等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用JavaMail实现激活示例

一、准备配置文件

mail.properties:

 Java Code 
1
2
3
4
5
6
7
host=邮件服务器
uname=用户名
pwd=密码
from=发件人
subject=主题
//邮件内容,一个超链接,链接到Servlet的激活方法,{0}是占位符,便于在Servlet中替换成激活码,
content=<a href="http://localhost:8080/bookstore/UserServlet?method=active&code={0}">\u70B9\u51FB\u8FD9\u91CC\u5B8C\u6210\u6FC0\u6D3B</a>

二、在Servlet中发邮件

1,准备配置文件

 Java Code 
1
2
3
4
5
6
7
8
9
10
11
Properties props = new Properties();
        props.load(
this.getClass().getClassLoader()
                .getResourceAsStream(
"email.properties"));    ///加载刚刚写的配置文件
        String host = props.getProperty("host");//获取服务器主机
        String uname = props.getProperty("uname");//获取用户名
        String pwd = props.getProperty("pwd");//获取密码
        String from = props.getProperty("from");//获取发件人
        String to = form.getEmail();//获取收件人
        String subject = props.getProperty("subject");//获取主题
        String content = props.getProperty("content");//获取邮件内容
        content = MessageFormat.format(content, form.getCode());//替换{0}

2,得到session,创建邮件对象,并发送邮件

 Java Code 
1
2
3
4
5
6
Session session = MailUtils.createSession(host, uname, pwd);//得到session
        Mail mail = new Mail(from, to, subject, content);//创建邮件对象
        try {
            MailUtils.send(session, mail);
//发邮件!
        } catch (MessagingException e) {
        }

三、保存成功信息,转发到msg.jsp

 Java Code 
1
2
3
4
5
6
/*
         * 1. 保存成功信息
         * 2. 转发到msg.jsp
         */

        request.setAttribute(
"msg""恭喜,注册成功!请马上到邮箱激活");
        
return "f:/jsps/msg.jsp";

四、激活完成

1,dao层实现

 Java Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
    /**
     * 按激活码查询
     * @param code
     * @return
     */

    
public User findByCode(String code) {
        
try {
            
String sql = "select * from tb_user where code=?";
            
return qr.query(sql, new BeanHandler<User>(User.class), code);
        } 
catch(SQLException e) {
            
throw new RuntimeException(e);
        }
    }
    
    
/**
     * 修改指定用户的指定状态
     * @param uid
     * @param state
     */

    
public void updateState(String uid, boolean state) {
        
try {
            
String sql = "update tb_user set state=? where uid=?";
            qr.update(sql, state, uid);
        } 
catch(SQLException e) {
            
throw new RuntimeException(e);
        }
    }

2,service层完成

 Java Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
     * 激活功能
     * @throws UserException 
     */

    
public void active(String code) throws UserException {
        
/*
         * 1. 使用code查询数据库,得到user
         */

        User user = userDao.findByCode(code);
        
/*
         * 2. 如果user不存在,说明激活码错误
         */

        
if(user == null) throw new UserException("激活码无效!");
        
/*
         * 3. 校验用户的状态是否为未激活状态,如果已激活,说明是二次激活,抛出异常
         */

        
if(user.isState()) throw new UserException("您已经激活过了,不要再激活了!");
        
/*
         * 4. 修改用户的状态
         */

        userDao.updateState(user.getUid(), true);
    }

3,Servlet层完成

 Java Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public String active(HttpServletRequest request, HttpServletResponse response)
            
throws ServletException, IOException {
        
/*
         * 1. 获取参数激活码
         * 2. 调用service方法完成激活
         *   > 保存异常信息到request域,转发到msg.jsp
         * 3. 保存成功信息到request域,转发到msg.jsp
         */

        
String code = request.getParameter("code");
        
try {
            userService.active(code);
            request.setAttribute(
"msg""恭喜,您激活成功了!请马上登录!");
        } 
catch (UserException e) {
            request.setAttribute(
"msg", e.getMessage());
        }
        
return "f:/jsps/msg.jsp";
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值