用户注册时,为了确保用户的有效性以及系统的安全性,通常需要用户填写邮箱,用户在登陆之前需要先到邮箱进行账号的激活操作。
本地演示时需要准备邮件服务器,可以使用eyoumailserversetup.exe,下载安装后软件如下:
安装之后可以右键新建账号进行测试。
准备邮件发送的util。
mail.properties:
mail_host = 127.0.0.1
admin_username = admin
admin_password = 1234
admin_domain = liu-pc.com
active_url = http://localhost:8080/ebookstore/client/activeUserServlet?activeUUID=
MailUtils:
package com.ebookstore.util;
import java.util.Properties;
import java.util.ResourceBundle;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import com.ebookstore.bean.User;
public class MailUtils {
/**
* 给指定用户发送邮件
*
* @param user
* @throws MessagingException
*/
public static void send(User user) throws MessagingException {
ResourceBundle bundle = ResourceBundle.getBundle("mail");
String mail_host = bundle.getString("mail_host"); // 主机地址
String admin_username = bundle.getString("admin_username"); // 发送邮件用户名
String admin_password = bundle.getString("admin_password"); // 发送邮件密码
String admin_domain = bundle.getString("admin_domain"); // 域名
String active_url = bundle.getString("active_url"); // 激活url地址
// 发邮件
// 0.1 基本参数
Properties props = new Properties();
props.setProperty("mail.host", mail_host);
props.setProperty("mail.smtp.auth", "true");
// 0.2 认证信息
Authenticator authenticator = new MyAuthenticator(admin_username,admin_password);
// 1 获得会话
Session session = Session.getDefaultInstance(props, authenticator);
// 2编写消息
Message message = new MimeMessage(session);
// 2.1 发件人
message.setFrom(new InternetAddress(admin_username + "@" + admin_domain)); // admin@lt.cn
// 2.2 收件人
message.setRecipient(RecipientType.TO,new InternetAddress(user.getEmail()));
// 2.3 主题
message.setSubject("“网络图书商城”用户注册激活邮件");
// 2.4 正文
String url = active_url + user.getActiveUUID();
String data = user.getUserName() + ",你好:<br/>"
+ " 您在本书城注册了用户,请<a href='" + url
+ "'>点击</a>激活,或复制下面的链接进行激活<br/>" + url + " <br/>"
+ "如果不是本人,请删除邮件";
message.setContent(data, "text/html;charset=UTF-8");
// 3 发送邮件
Transport.send(message);
}
}
class MyAuthenticator extends Authenticator {
private String admin_password;
private String admin_username;
public MyAuthenticator(String admin_username, String admin_password) {
this.admin_username = admin_username;
this.admin_password = admin_password;
}
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(admin_username, admin_password);
}
}
注册用户时在数据存入数据库之前发送邮件:
public boolean registerUser(User user) {
Connection conn = null;
try {
conn = JdbcUtils.getConnection();
//# 1 开启事务
conn.setAutoCommit(false);
//发送激活邮件
MailUtils.send(user);
//添加
boolean r = userDao.save(conn,user);
//# 2 提交事务
conn.commit();
return r;
} catch (Exception e) {
//# 3 回滚事务
DbUtils.rollbackAndCloseQuietly(conn);
throw new RuntimeException(e.getMessage(),e);
}
}
激活处理的servlet:
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//1 获得数据
String activeUUID = request.getParameter("activeUUID");
//2 执行操作--激活
BusinessService service = new BusinessService();
boolean returnMsg = service.activeUser(activeUUID);
//3提示
if(returnMsg){
request.setAttribute("message", "用户激活成功,请<a href='#'>登录</a>");
} else {
request.setAttribute("message", "用户没有激活成功,请重新激活");
}
//转发
request.getRequestDispatcher("/message.jsp").forward(request, response);
}
邮箱显示效果: