1. 需求
现在需要在用户注册的时候,注册时给用户发送一条注册成功的邮件(异步发送, @Async注解) 在用户修改密码的时候,发送一条验证码邮件(1分钟内只能发送一次,15分钟有效),并实现密码修改
2. 实现
2.1 依赖
<!--发送邮件的jar包-->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-dm</artifactId>
<version>3.1.0</version>
</dependency>
2.2 UserServiceimpl
@Resource
private ThreadTasks threadTasks;
@Override
public String register(RegisterUserVo vo) {
threadTasks.sendRegisteMail(sysUser);
}
@Override
public String verify(String email) {
int count = userMapper.checkUserExist(email);
if (count == 0) {
return "用户名或密码错误,请重新输入!";
}
String s = redisTemplate.opsForValue().get(RedisConstant.SAAS_VERIFICATION_CODE_EXIST + email);
if (!StringUtils.isEmpty(s)) {
return "您的操作太频繁了,请稍后再试。";
}
String code = randomCode();
redisTemplate.opsForValue().set(RedisConstant.SAAS_VERIFICATION_CODE_EXIST + email, code, 1, TimeUnit.MINUTES);
redisTemplate.opsForValue().set(RedisConstant.SAAS_VERIFICATION_CODE + email, code, 15, TimeUnit.MINUTES);
sendEmaiUtil.sendCodeMail(code, email);
return null;
}
@Override
public String resetPassword(RegisterUserVo vo) {
String s = redisTemplate.opsForValue().get(RedisConstant.SAAS_VERIFICATION_CODE + vo.getEmail());
if (!vo.getCode().equals(s)) {
return "验证码有误!";
}
SysUser user = new SysUser();
user.setPassword(MD5Util.generate(vo.getPassword()));
user.setLoginId(vo.getEmail());
userMapper.resetPassword(user);
return null;
}
2.3 异步发送邮件
import com.ymob.saas.managerserver.pojo.sys.SysUser;
import com.ymob.saas.managerserver.utils.SendEmaiUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
@Transactional
@Component
@Slf4j
public class ThreadTasks {
@Resource
private SendEmaiUtil sendEmaiUtil;
@Async
public void sendRegisteMail(SysUser sysUser) {
sendEmaiUtil.sendRegisterMail(sysUser);
}
2.4 发送邮件
2.4.1 发送注册邮件
SendEmaiUtil
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dm.model.v20151123.SingleSendMailRequest;
import com.aliyuncs.dm.model.v20151123.SingleSendMailResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.ymob.saas.managerserver.config.SaasConfig;
import com.ymob.saas.managerserver.pojo.sys.SysUser;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@Component
public class SendEmaiUtil {
@Resource
private SaasConfig saasConfig;
public void sendRegisterMail(SysUser sysUser) {
String str = "<p> 尊敬的【username】:</p ><nav></nav><p> 您好,感谢您对" + saasConfig.getName() + "媒体服务平台的信任和支持!</p ><nav></nav><p> 请登录" + saasConfig.getName() + "平台开启您的收益提升之旅,登录地址: https://app.medproad.com</p ><nav></nav><p> 如果您有其他问题,请通过support@medproad.com与我们联系</p ><p> </p ><nav></nav><p> Medpro团队</p ><nav></nav>";
IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI5tKx8r7TwRH3Arqttna9", "laMaU8QYkKwCF9WKpSfdj8AKlxyNn8");
IAcsClient client = new DefaultAcsClient(profile);
SingleSendMailRequest request = new SingleSendMailRequest();
try {
request.setAccountName(saasConfig.getEmailAccount());
request.setFromAlias(saasConfig.getName());
request.setAddressType(1);
request.setReplyToAddress(true);
request.setToAddress(sysUser.getEmail());
request.setSubject("欢迎注册" + saasConfig.getName() + "媒体服务平台,我们等您很久了!");
String htmlBody = str.replaceAll("username", sysUser.getUsername());
request.setHtmlBody(htmlBody);
request.setMethod(MethodType.POST);
SingleSendMailResponse httpResponse = client.getAcsResponse(request);
} catch (ClientException e) {
System.out.println("ErrCode : " + e.getErrCode());
e.printStackTrace();
}
}
public void sendCodeMail(String code, String email) {
IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI5tKx8r7TwRH3Arqttna9", "laMaU8QYkKwCF9WKpSfdj8AKlxyNn8");
IAcsClient client = new DefaultAcsClient(profile);
SingleSendMailRequest request = new SingleSendMailRequest();
String str =
"<p> 您好,您正在修改" + saasConfig.getName() + "登录密码,请在验证码输入框输入: 【code】 ,验证码有效期\n" +
" 为15分钟,请尽快完成操作。验证码提供给他人可能导致账号被盗,请勿泄露,谨防被骗。</p >" +
"<p> " + saasConfig.getName() + "团队</p >";
try {
request.setAccountName(saasConfig.getEmailAccount());
request.setFromAlias(saasConfig.getName());
request.setAddressType(1);
request.setReplyToAddress(true);
request.setToAddress(email);
request.setSubject(saasConfig.getName() + "账号密码重置");
String htmlBody = str.replaceAll("code", code);
request.setHtmlBody(htmlBody);
request.setMethod(MethodType.POST);
SingleSendMailResponse httpResponse = client.getAcsResponse(request);
} catch (ClientException e) {
System.out.println("ErrCode : " + e.getErrCode());
e.printStackTrace();
}
}
}
|