郭贤达的博客

天地大矣,我心辽矣;恰同学少年,来日方长!

博客园 首页 新随笔 联系 订阅 管理

 创建邮件发送工具类:

package cn.itcast.shop.user.utils;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
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;
import javax.mail.internet.MimeMessage.RecipientType;

/*
 * 邮件发送工具类
 */
public class MailUtils {
    /*
     * 发送邮件的方法
     * @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.21.75:8080/shop/user_active.action?code="+code+"'>http://192.168.21.75: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("ccc@shop.com","11111111111111");
    }
}

Struts层代码:

<!-- 全局页面 -->
  <result name="msg">/WEB-INF/jsp/msg.jsp</result>
</global-results>

Action层代码:

/**
* 用户注册的方法:
*/
    public String regist() {
        // 判断验证码程序:
        // 从session中获得验证码的随机值:
        String checkcode1 = (String) ServletActionContext.getRequest()
                .getSession().getAttribute("checkcode");
        if(!checkcode.equalsIgnoreCase(checkcode1)){
            this.addActionError("验证码输入错误!");
            return "checkcodeFail";
        }
        userService.save(user);
        this.addActionMessage("注册成功!请去邮箱激活!");
        return "msg";
    }

Service层代码:

package cn.itcast.shop.user.service;
import org.springframework.transaction.annotation.Transactional;
import cn.itcast.shop.user.dao.UserDao;
import cn.itcast.shop.user.utils.MailUtils;
import cn.itcast.shop.user.utils.UUIDUtils;
import cn.itcast.shop.user.vo.User;

/*
 * 用户模块业务层的代码
 */
@Transactional
public class UserService {
    //注入UserDao
    private UserDao userDao;

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }
    
    //业务层完成用户注册代码
    public void save(User user) {
        //将数据存入到数据库
        user.setState(0); //0:代表用户未激活     1:代表用户已经激活
        //调用工具类生成字符串
        String code=UUIDUtils.getUUID()+UUIDUtils.getUUID();
        user.setCode(code);
        userDao.save(user);
        //发送激活邮件
        MailUtils.sendMail(user.getEmail(),code);
    }
}

 

posted on 2016-07-16 15:43  陀螺ING  阅读(268)  评论(0)    收藏  举报