发送激活邮件

来源:互联网 发布:全国地区信息 数据库 编辑:程序博客网 时间:2024/05/16 02:02

     我们每天都会接收,发送邮件,今天就来讲一讲邮件发送的原理以及如何实现。

邮件发送的原理

基本概念

邮件服务器:跟web服务器一样,我们每一个电子邮箱的所有信息都会保存在邮件服务器上,在web服务器上也保存着各种各样的网页信息。

邮件协议:还记得我们配置Foxmail时的SMTP协议和POP3协议吗?SMTP就是发送邮件的协议。而POP是接收邮件的协议,现在常用的是第三版,所以也称POP3协议。

原理过程

 

     假如用户abc@sina.com想要给abc@163.com发送一封邮件,具体过程为:

abc@sina.com给abc@163.com发送邮件

1.登录邮箱客户端

2.连接新浪的SMTP服务器

3.编写邮件并保存到本地

4.发送到163的SMTP服务器上

5.163的SMTP服务器保存邮件

abc@163.com接收邮件

6.登录邮箱客户端

7.连接到163的POP3服务器

8.找到服务器存储邮件的位置

9.收到邮件并返回到客户端

    这样就实现了邮件的接收和发送。


    我们在网站上注册账号的时候,一般都会收到一封激活邮件,在邮箱里点击激活链接后才能登录到网站上。所以发送激活邮件的大体思路是:

    首先向用户邮箱中发送激活邮件,邮件内容为激活账号的连接,链接内容包括发送邮件的IP和激活码。

    接下来就是用户激活:在邮件中点击激活链接,后台则根据传递的激活码进行用户查询,如果激活码不为空,则修改用户状态,即可以登录网站。

代码实现

首先引入jar包:mail.jar和activation.jar

发送邮件的方法

/** * 发送邮件的方法 * @param to:收件人 * @param code:激活码 * @throws MessagingException  * @throws AddressException  */public static void sendMail(String to,String code) throws AddressException, MessagingException{/** * 1.获得一个Session对象 * 2.创建一个代表邮件的对象 Message * 3.发送邮件 Transport *///1.获得连接对象Properties props = new Properties();props.setProperty("mail.host", "localhost");Session session = Session.getInstance(props, new Authenticator(){@Overrideprotected PasswordAuthentication getPasswordAuthentication() {// TODO Auto-generated method stubreturn 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("来自TGB官方商城激活邮件");//设置邮件正文message.setContent("<h1>TGB官方商城激活邮件!点下面连接完成激活操作</h1><h3><a href='http://192.168.21.170:8080/shop/user_active.action?code="+code+"'>http://192.168.21.170: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();}}
        //调用sendMail,发送邮件public static void main(String[] args){try {sendMail("aaa@shop.com","11111111");} catch (AddressException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (MessagingException e) {// TODO Auto-generated catch blocke.printStackTrace();}}

收到的邮件效果


用户激活

Action中用户激活的方法

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

Service中的方法

<span style="font-size:18px;">        //业务层根据激活码查询用户public User fineByCode(String code) {return userDao.findByCode(code);}//修改用户状态的方法public void update(User existUser) {userDao.update(existUser);}</span>
Dao中的方法

<span style="font-size:18px;">        //根据激活码查询用户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>
          这样我们就实现了发送激活邮件并激活的功能,理清思路再去做就OK了。

0 1