邮箱注册
javaMail简介
javax.mail
mail
1.4.7
public class SendEmail
{
public static void main(String [] args)
{
// 收件人电子邮箱
String to = "[email protected]";
// 发件人电子邮箱
String from = "[email protected]";
// 指定发送邮件的主机为 localhost
String host = "localhost";
// 获取系统属性
Properties properties = System.getProperties();
// 设置邮件服务器
properties.setProperty("mail.smtp.host", host);
// 获取默认session对象
Session session = Session.getDefaultInstance(properties);
try{
// 创建默认的 MimeMessage 对象
MimeMessage message = new MimeMessage(session);
// Set From: 头部头字段
message.setFrom(new InternetAddress(from));
// Set To: 头部头字段
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
// Set Subject: 头部头字段
message.setSubject("This is the Subject Line!");
// 设置消息体
message.setText("This is actual message");
// 发送消息
Transport.send(message);
System.out.println("Sent message successfully....");
Transport.close();
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
Spring Mail API
详细步骤
void itriptxCreateByMail(ItripUser user) throws Exception;
void sendActivationMail(String mailTo, String activationCode);
@Override
public void itriptxCreateByMail(ItripUser user) throws Exception {
// 添加用户信息
itripUserMapper.insertItripUser(user);
// 生成激活码
String activationCode = MD5.getMd5(user.getUserCode(), 32);
// 发送邮件
mailService.sendActivationMail(user.getUserCode(), activationCode);
// 激活码存入redis
redisAPI.set("activation:" + user.getUserCode(), activationCode, 30 * 60);
}
@Service
public class MailServiceImpl implements MailService {
@Autowired
private MailSender mailSender;
@Autowired
private SimpleMailMessage mailMessage;
/**
* 发送注册激活邮件
*/
public void sendActivationMail(String mailTo, String activationCode) {
mailMessage.setTo(mailTo);
mailMessage.setText("注册邮箱:" + mailTo + " 激活码:" + activationCode);
mailSender.send(mailMessage);
}
}
boolean activateByMail(String email, String code) throws Exception;
@Override
public boolean activateByMail(String email, String code) throws Exception {
// 验证激活码
String key = "activation:" + email;
if (redisAPI.exists(key)) {
if (redisAPI.get(key).equals(code)) {
ItripUser itripUser = findByUsername(email);
if (EmptyUtils.isNotEmpty(itripUser)) {
itripUser.setActivated(1);//激活用户
itripUser.setUserType(0);//自注册用户
itripUser.setFlatID(itripUser.getId());
itripUserMapper.updateItripUser(itripUser);
return true;
}
}
}
return false;
}
@Controller
@RequestMapping(value = "api")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping(value = "/registerByMail", method = RequestMethod.POST, produces = "application/json")
public @ResponseBody
Dto registerByMail(@RequestBody ItripUserVO userVO) {
if (!validEmail(userVO.getUserCode()))
return DtoUtil.returnFail("请使用正确的邮箱地址注册", ErrorCode.AUTH_ILLEGAL_USERCODE);
try {
if (null == userService.findByUsername(userVO.getUserCode())) {
ItripUser user = new ItripUser();
user.setUserCode(userVO.getUserCode());
user.setUserName(userVO.getUserName());
user.setUserType(0);
user.setUserPassword(MD5.getMd5(user.getUserPassword(), 32));
userService.itriptxCreateByMail(user);
return DtoUtil.returnSuccess();
} else {
return DtoUtil.returnFail("用户已存在,注册失败", ErrorCode.AUTH_USER_ALREADY_EXISTS);
}
} catch (Exception e) {
e.printStackTrace();
return DtoUtil.returnFail(e.getMessage(), ErrorCode.AUTH_UNKNOWN);
}
}
@RequestMapping(value = "/activateByMail", method = RequestMethod.PUT, produces = "application/json")
@ResponseBody
public Dto activateByMail(@RequestParam String email, @RequestParam String code) {
try {
if (userService.activateByMail(email, code)) {
return DtoUtil.returnSuccess("激活成功");
} else {
return DtoUtil.returnSuccess("激活失败");
}
} catch (Exception e) {
e.printStackTrace();
return DtoUtil.returnFail("激活失败", ErrorCode.AUTH_ACTIVATE_FAILED);
}
}
/**
* 合法E-mail地址:
* 1. 必须包含一个并且只有一个符号“@”
* 2. 第一个字符不得是“@”或者“.”
* 3. 不允许出现“@.”或者.@
* 4. 结尾不得是字符“@”或者“.”
* 5. 允许“@”前的字符中出现“+”
* 6. 不允许“+”在最前面,或者“+@”
*/
private boolean validEmail(String email) {
String regex = "^\\s*\\w+(?:\\.{0,1}[\\w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\\.[a-zA-Z]+\\s*$";
return Pattern.compile(regex).matcher(email).find();
}