2
0

feat: 邮件内容自定义html

This commit is contained in:
caiyuchao
2025-04-16 14:09:20 +08:00
parent e8b672f119
commit 4f5cd0c0ce
6 changed files with 162 additions and 2 deletions

View File

@@ -90,6 +90,11 @@
<artifactId>mapstruct</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
<build>

View File

@@ -8,6 +8,8 @@ import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.wfc.common.core.constant.Constants;
import org.wfc.common.core.constant.GlobalConstants;
import org.wfc.common.core.domain.R;
@@ -34,6 +36,7 @@ public class SysEmailController extends BaseController {
private final MailProperties mailProperties;
private final RedisService redisService;
private final TemplateEngine templateEngine;
/**
* 邮箱验证码
@@ -49,7 +52,11 @@ public class SysEmailController extends BaseController {
String code = RandomUtil.randomNumbers(4);
redisService.setCacheObject(key, code, Constants.MAIL_CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
try {
MailUtils.sendText(email, "Your WANFI verification code", "Hi\nYour verification code is:\n" + code + "\nThis code will expire in " + Constants.MAIL_CAPTCHA_EXPIRATION + " minutes.\nFor your security, don't share it with anyone.");
Context context = new Context();
context.setVariable("verificationCode", code);
context.setVariable("expirationTime", Constants.MAIL_CAPTCHA_EXPIRATION);
String htmlStr = templateEngine.process("mail", context);
MailUtils.sendHtml(email, "Your WANFI verification code", htmlStr);
} catch (Exception e) {
log.error("email verification code send failed => {}", e.getMessage());
return R.fail(e.getMessage());

View File

@@ -0,0 +1,68 @@
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>验证码邮件</title>
<style>
/* 内联样式兼容邮件客户端 */
@media only screen and (max-width: 600px) {
.container {
width: 100% !important;
}
.code {
font-size: 24px !important;
}
}
</style>
</head>
<body style="margin: 0; padding: 0; font-family: Arial, sans-serif; background-color: #f5f5f5;">
<table role="presentation" width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="center" style="padding: 20px 0;">
<!-- 容器开始 -->
<table class="container" width="600" border="0" cellspacing="0" cellpadding="0" style="background-color: #ffffff; border-radius: 8px; box-shadow: 0 2px 10px rgba(0,0,0,0.1);">
<!-- 头部 -->
<tr>
<td style="padding: 30px 20px; border-bottom: 1px solid #eeeeee;">
<img src="[您的LogoURL]" alt="公司Logo" width="120" style="display: block; margin: 0 auto;">
</td>
</tr>
<!-- 内容区 -->
<tr>
<td style="padding: 40px 30px;">
<h2 style="color: #333333; margin: 0 0 20px 0;">您的验证码</h2>
<p style="color: #666666; line-height: 1.6; margin: 0 0 20px 0;">
请使用以下验证码完成操作:
</p>
<div style="background-color: #f8f9fa; padding: 15px; border-radius: 4px; text-align: center; margin: 20px 0;">
<strong class="code" style="font-size: 28px; color: #2d3748; letter-spacing: 3px;"> <span th:text="${verificationCode}"></span> </strong>
</div>
<p style="color: #999999; font-size: 12px; margin: 20px 0;">
该验证码将在<span th:text="${expirationTime}"></span> 分钟后失效,请及时使用
</p>
</td>
</tr>
<!-- 底部 -->
<tr>
<td style="padding: 20px 30px; background-color: #f8f9fa; border-radius: 0 0 8px 8px;">
<p style="color: #999999; font-size: 12px; margin: 0;">
此为系统自动发送邮件,请勿直接回复<br>
如有疑问请联系 <a href="mailto:support@example.com" style="color: #4299e1;">support@example.com</a><br>
© 2023 您的公司名称。保留所有权利
</p>
<p style="color: #999999; font-size: 12px; margin: 10px 0 0 0;">
<a href="{{unsubscribe_link}}" style="color: #718096; text-decoration: none;">退订邮件</a>
</p>
</td>
</tr>
</table>
<!-- 容器结束 -->
</td>
</tr>
</table>
</body>
</html>

View File

@@ -103,6 +103,11 @@
<groupId>org.wfc</groupId>
<artifactId>wfc-common-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
<build>

View File

@@ -8,6 +8,8 @@ import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.wfc.common.core.constant.Constants;
import org.wfc.common.core.constant.GlobalConstants;
import org.wfc.common.core.domain.R;
@@ -34,6 +36,7 @@ public class UEmailController extends BaseController {
private final MailProperties mailProperties;
private final RedisService redisService;
private final TemplateEngine templateEngine;
/**
* 邮箱验证码
@@ -49,7 +52,11 @@ public class UEmailController extends BaseController {
String code = RandomUtil.randomNumbers(4);
redisService.setCacheObject(key, code, Constants.MAIL_CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
try {
MailUtils.sendText(email, "Your WANFI verification code", "Hi\nYour verification code is:\n" + code + "\nThis code will expire in " + Constants.MAIL_CAPTCHA_EXPIRATION + " minutes.\nFor your security, don't share it with anyone.");
Context context = new Context();
context.setVariable("verificationCode", code);
context.setVariable("expirationTime", Constants.MAIL_CAPTCHA_EXPIRATION);
String htmlStr = templateEngine.process("mail", context);
MailUtils.sendHtml(email, "Your WANFI verification code", htmlStr);
} catch (Exception e) {
log.error("email verification code send failed => {}", e.getMessage());
return R.fail(e.getMessage());

View File

@@ -0,0 +1,68 @@
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>验证码邮件</title>
<style>
/* 内联样式兼容邮件客户端 */
@media only screen and (max-width: 600px) {
.container {
width: 100% !important;
}
.code {
font-size: 24px !important;
}
}
</style>
</head>
<body style="margin: 0; padding: 0; font-family: Arial, sans-serif; background-color: #f5f5f5;">
<table role="presentation" width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="center" style="padding: 20px 0;">
<!-- 容器开始 -->
<table class="container" width="600" border="0" cellspacing="0" cellpadding="0" style="background-color: #ffffff; border-radius: 8px; box-shadow: 0 2px 10px rgba(0,0,0,0.1);">
<!-- 头部 -->
<tr>
<td style="padding: 30px 20px; border-bottom: 1px solid #eeeeee;">
<img src="[您的LogoURL]" alt="公司Logo" width="120" style="display: block; margin: 0 auto;">
</td>
</tr>
<!-- 内容区 -->
<tr>
<td style="padding: 40px 30px;">
<h2 style="color: #333333; margin: 0 0 20px 0;">您的验证码</h2>
<p style="color: #666666; line-height: 1.6; margin: 0 0 20px 0;">
请使用以下验证码完成操作:
</p>
<div style="background-color: #f8f9fa; padding: 15px; border-radius: 4px; text-align: center; margin: 20px 0;">
<strong class="code" style="font-size: 28px; color: #2d3748; letter-spacing: 3px;"> <span th:text="${verificationCode}"></span> </strong>
</div>
<p style="color: #999999; font-size: 12px; margin: 20px 0;">
该验证码将在<span th:text="${expirationTime}"></span> 分钟后失效,请及时使用
</p>
</td>
</tr>
<!-- 底部 -->
<tr>
<td style="padding: 20px 30px; background-color: #f8f9fa; border-radius: 0 0 8px 8px;">
<p style="color: #999999; font-size: 12px; margin: 0;">
此为系统自动发送邮件,请勿直接回复<br>
如有疑问请联系 <a href="mailto:support@example.com" style="color: #4299e1;">support@example.com</a><br>
© 2023 您的公司名称。保留所有权利
</p>
<p style="color: #999999; font-size: 12px; margin: 10px 0 0 0;">
<a href="{{unsubscribe_link}}" style="color: #718096; text-decoration: none;">退订邮件</a>
</p>
</td>
</tr>
</table>
<!-- 容器结束 -->
</td>
</tr>
</table>
</body>
</html>