1.修改邮件发送为异步处理

This commit is contained in:
2025-08-13 21:58:51 +08:00
parent a2172c691b
commit 918ada9a82

View File

@@ -1,6 +1,7 @@
package vvpkassistant.mail.service;
import cn.dev33.satoken.temp.SaTempUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.extra.mail.MailUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
@@ -56,6 +57,8 @@ public class MailServiceImpl implements MailService {
if (emailSendCache.getIfPresent(emailAddress) != null) {
throw new BusinessException(ErrorCode.EMAIL_SEND_FREQUENT);
}
ThreadUtil.execute(() -> {
try {
MailUtil.send(emailAddress, "激活你的账号", "<!DOCTYPE html>\n" +
"<html lang=\"en\">\n" +
"<head>\n" +
@@ -210,6 +213,11 @@ public class MailServiceImpl implements MailService {
" </div>\n" +
"</body>\n" +
"</html>\n", true);
log.info("账号激活邮件发送成功,邮箱地址:{},用户 Id{}", emailAddress,userId);
} catch (Exception e) {
log.error("账号激活邮件发送失败,邮箱地址:{},用户 Id{}",emailAddress,userId);
}
});
emailSendCache.put(emailAddress, userId);
}
@@ -220,6 +228,8 @@ public class MailServiceImpl implements MailService {
if (emailSendCache.getIfPresent(emailAddress) != null) {
throw new BusinessException(ErrorCode.EMAIL_SEND_FREQUENT);
}
ThreadUtil.execAsync(() ->{
try {
MailUtil.send(emailAddress, "验证你的邮箱", "<!DOCTYPE html>\n" +
"<html lang=\"en\">\n" +
"<head>\n" +
@@ -374,6 +384,12 @@ public class MailServiceImpl implements MailService {
" </div>\n" +
"</body>\n" +
"</html>\n", true);
log.info("邮箱验证发送失败,邮箱地址:{},用户 Id: {}",emailAddress,userId );
}catch (Exception e) {
log.error("邮箱验证发送失败,邮箱地址:{},用户 Id: {}",emailAddress,userId );
}
});
emailSendCache.put(emailAddress, userId);
}
@@ -410,6 +426,8 @@ public class MailServiceImpl implements MailService {
public void sendForgetPassWordMail(String mailAddress, Integer userId) {
if (checkCache(mailAddress)){
String token = SaTempUtil.createToken(userId, 600);
ThreadUtil.execAsync(() ->{
try {
MailUtil.send(mailAddress, "验证你的邮箱", "<!DOCTYPE html>\n" +
"<html lang=\"en\">\n" +
"<head>\n" +
@@ -564,6 +582,11 @@ public class MailServiceImpl implements MailService {
" </div>\n" +
"</body>\n" +
"</html>\n", true);
log.info("找回密码邮件发送成功,邮箱地址:{},用户 Id{}", mailAddress,userId);
}catch (Exception e) {
log.error("找回密码邮件发送失败,邮箱地址:{},用户 Id{}", mailAddress,userId);
}
});
emailSendCache.put(mailAddress, userId);
}
@@ -583,9 +606,15 @@ public class MailServiceImpl implements MailService {
String code = RandomUtil.randomString(6);
emailSendCache.put(mailModel.getMailAddress(), code);
CacheHolder.VERIFICATION_MAIL.put(code, mailModel.getMailAddress());
ThreadUtil.execAsync(() -> {
try {
MailUtil.send(mailModel.getMailAddress(),"your Verification code is :" + code,
"your Verification code is :" + code,false);
log.info("sendMailto:{},Verification code is :{}", mailModel.getMailAddress(),code);
}catch (Exception e){
log.error("发送邮件失败, address={}, code={}", mailModel.getMailAddress(), code, e);
}
});
}
return true;
}