一、目标
对注册时填写的邮箱进行激活,当注册成功以后,会显示一个前往激活邮箱的按钮,点击即可登录邮箱激活.
- 情况1:修改了邮箱账号或者激活码,激活失败error;
- 情况2:在指定时间内未激活,激活失败,重新获取激活码再次激活error;
- 情况3:在符合条件的情况下,连续两次激活,第二次激活无效,重复激活error;
- 情况4:按要求操作,激活成功success.
二、业务流程图:
三、实现步骤:
1 添加maven依赖,项目由maven管理
<!--javamail begin--><dependency><groupId>javax.mail</groupId><artifactId>mail</artifactId><version>1.4.1</version></dependency><!--javamail end-->
2 继承Authenticator类编写发送消息的方法,附注释
package com.changhong.camp.cmms.util;import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.Properties;/*** Created by 1250052380@qq.com on 2014/11/28.* 这里为了方便起见,将发送邮件的方法封装在这个授权认证的类里。* sendEmailActivationCode(String uuid, String email)方法就是向指定的收件箱email发送随机激活码uuid。*/
public class EmailAuthenticator extends Authenticator {private String username;private String password;public EmailAuthenticator(String username, String password) {this.username = username;this.password = password;}@Overrideprotected PasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication(username, password);}/*** 发送邮件的方法** @param uuid 发送随机激活码* @param email 目标邮箱地址*/public static void sendEmailActivationCode(String uuid, String email) {//todo/*初始化一个发件人*//*发件人邮箱账号*/String username = "15196271260@163.com";/*发件人邮箱密码*/String password = "123456";/*构造一个已授权认证的发件人对象*/Authenticator authenticator = new EmailAuthenticator(username, password);/*action代表的是动作,即用户点击邮箱的超链接所发送的请求,包括请求服务器controller的方法,并且携带两个参数*/String action = "http://localhost:8080/developer/active/email?email=" + email + "&code=" + uuid;/*构造一个包含激活邮箱动作的超链接*/String url = "<a href=\"" + action + "\" target=\"_self\">" + action + "</a>";/*具体的邮件内容*/String msg = "请在30分钟内点击链接激活邮箱:" + url;/*设置发送方服务器参数,163邮箱服务器是smtp.163.com,其他邮箱可查阅官方文档。*/Properties props = new Properties();props.setProperty("mail.smtp.host", "smtp.163.com");props.setProperty("mail.smtp.auth", "true");javax.mail.Session session = javax.mail.Session.getDefaultInstance(props, authenticator);
// session.setDebug(true);try {/*构造一个Address发件人*/Address from = new InternetAddress(username);/*构造一个Address收件人*/Address to = new InternetAddress(email);/*构造一个电子邮件对象*MimeMessage参考博客:http://blog.csdn.net/yanan_seachange/article/details/8474922*/MimeMessage mimeMessage = new MimeMessage(session);/*设置消息发送者*/mimeMessage.setFrom(from);/*邮件主题*/mimeMessage.setSubject("邮箱激活");/*邮件发送日期*/mimeMessage.setSentDate(new Date());/*邮件的内容以及内容的类型和编码*/mimeMessage.setContent(msg, "text/html;charset=utf-8");mimeMessage.setRecipient(Message.RecipientType.TO, to);/*执行发送邮件*/Transport.send(mimeMessage);} catch (MessagingException e) {e.printStackTrace();}}
}
3 在注册的时候调用发送邮件的方法并将随机激活码存入到数据库
EmailAuthenticator.sendEmailActivationCode(uuid, userDto.getEmail());
4 当发送激活码以后,假如用户没有即使去激活,在将来激活的时候需要再次获取激活码,新加一个方法用来再次发送激活码
/*** @param email 接收激活链接的邮箱* @return*/@RequestMapping(value = "active/make", method = RequestMethod.POST)@ResponseBodypublic ResponseEntity<Object> makeActivationCode(@RequestParam(value = "email") String email) {try {UserDto userDto = developerService.findByEmail(email);String uuid = StringUtils.generateUuidString();userDto.setEmailActivationCode(uuid);developerService.update(userDto);EmailAuthenticator.sendEmailActivationCode(uuid, userDto.getEmail());return new ResponseEntity(null, HttpStatus.OK);} catch (CoreException e) {e.printStackTrace();return new ResponseEntity(null, HttpStatus.BAD_REQUEST);}}
5 当用户登陆邮箱点击激活超链接时会以get方式请求服务器,服务器的激活处理
用户前往邮箱查看邮件,点击超链接发送请求携带的参数为用户邮箱账号和服务器发送的激活码,请求发送至第5步。不同情况会转到不同的页面。
/*** 处理用户点击链接后的激活动作** @param email 激活邮箱账号* @param code 激活码* @return 激活之后的指定页面*/@RequestMapping(value = "active/email", method = RequestMethod.GET)public String handleActiveEmail(@RequestParam(value = "email") String email,@RequestParam(value = "code") String code,HttpServletResponse response) {try {UserDto userDto = developerService.findByEmail(email);Date now = new Date();long interval = (now.getTime() - userDto.getUpdateTime().getTime()) / (1000 * 60);if (userDto == null) {//账号不存在} else if (interval <= 1) {System.out.println(interval);//30分钟内激活if (userDto.getEmailActivationCode().equals(code.trim()) && userDto.getEmailVerified() == false) {userDto.setEmailVerified(true);developerService.update(userDto);return "developer/register";} else if (userDto.getEmailActivationCode().equals(code.trim()) && userDto.getEmailVerified() == true) {//已经激活过了,暂且返回该页面return "developer/duplicate";} else {//激活码不正确,验证失败,暂且返回该页面return "developer/error";}} else {//超过指定时间未激活,重新获取激活码return "developer/reactive";}} catch (CoreException e) {e.printStackTrace();}return "developer/error";}
6 注册页面,填写相关信息并注册,表单的验证这里忽略!
7 注册成功以后,,提交请求至第3步,会显示激活按钮,提示前往激活
四、总结
主要工作:查看邮件服务器的参数、添加maven依赖、编写邮件发送方法、controller的处理请求的方法(包括注册、获取激活码、激活事件处理),页面的ajax提交表单等。本人属新人