fix: 优化邮件发送服务

- 邮件发送者名称设置为"AIOJ"
- 添加UnsupportedEncodingException异常处理
- 新增RedisKeyConstants常量类统一管理Redis Key

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2026-01-09 23:57:09 +08:00
parent 47a468096d
commit 7aacad2596
2 changed files with 20 additions and 1 deletions

View File

@@ -0,0 +1,16 @@
package cn.meowrain.aioj.backend.userservice.common.constants;
/**
* Redis Key 常量对象 - user-service
*/
public class RedisKeyConstants {
/**
* 邮箱验证码存储 Key 格式: email:code:{email}
*/
public static final String EMAIL_CODE_PREFIX = "email:code:%s";
/**
* 邮箱验证码有效期 (秒) - 1分钟
*/
public static final long EMAIL_CODE_TTL = 60;
}

View File

@@ -13,6 +13,7 @@ import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.io.UnsupportedEncodingException;
import java.security.SecureRandom; import java.security.SecureRandom;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@@ -107,7 +108,7 @@ public class EmailServiceImpl implements EmailService {
MimeMessage message = mailSender.createMimeMessage(); MimeMessage message = mailSender.createMimeMessage();
// ⭐ 显式指定 UTF-8 编码,解决中文乱码问题 // ⭐ 显式指定 UTF-8 编码,解决中文乱码问题
MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8"); MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
helper.setFrom(FROM_EMAIL); helper.setFrom(FROM_EMAIL,"AIOJ");
helper.setTo(email); helper.setTo(email);
helper.setSubject(SUBJECT); helper.setSubject(SUBJECT);
helper.setText(buildEmailContent(code), true); helper.setText(buildEmailContent(code), true);
@@ -118,6 +119,8 @@ public class EmailServiceImpl implements EmailService {
}catch (MessagingException e) { }catch (MessagingException e) {
log.error("验证码邮件发送失败: 邮箱={}", email, e); log.error("验证码邮件发送失败: 邮箱={}", email, e);
throw new ServiceException("邮件发送失败,请稍后重试"); throw new ServiceException("邮件发送失败,请稍后重试");
} catch (UnsupportedEncodingException e) {
throw new ServiceException("邮件发送失败,请稍后重试");
} }
} }