修复:优化消息中心 SMTP 校验提示

This commit is contained in:
xiaowang
2026-03-23 17:17:23 +08:00
parent bfa92ba2ce
commit 44ce34f762
5 changed files with 121 additions and 2 deletions
@@ -24,12 +24,14 @@ public class SmtpEmailChannelProvider implements EmailChannelProvider {
@Override
public void validate(ManagedChannelConfig channelConfig) {
requireText(channelConfig.getStringConfig("host"), "SMTP 主机不能为空");
if (channelConfig.getIntConfig("port", 0) <= 0) {
int port = channelConfig.getIntConfig("port", 0);
if (port <= 0) {
throw new IllegalArgumentException("SMTP 端口不能为空");
}
requireText(channelConfig.getStringConfig("username"), "SMTP 用户名不能为空");
requireText(channelConfig.getStringConfig("fromAddress"), "发件人地址不能为空");
requireText(channelConfig.getStringSecret("password"), "SMTP 密码不能为空");
validateSecurityMode(channelConfig, port);
}
@Override
@@ -54,6 +56,7 @@ public class SmtpEmailChannelProvider implements EmailChannelProvider {
@Override
public ChannelHealthCheckResult checkHealth(ManagedChannelConfig channelConfig) {
try {
validate(channelConfig);
Session session = buildSession(channelConfig);
Transport transport = session.getTransport("smtp");
transport.connect(
@@ -75,7 +78,10 @@ public class SmtpEmailChannelProvider implements EmailChannelProvider {
props.put("mail.smtp.port", channelConfig.getIntConfig("port", 25));
props.put("mail.smtp.auth", channelConfig.getBooleanConfig("auth", true));
props.put("mail.smtp.starttls.enable", channelConfig.getBooleanConfig("starttls", true));
props.put("mail.smtp.starttls.required", channelConfig.getBooleanConfig("starttls", true));
props.put("mail.smtp.ssl.enable", channelConfig.getBooleanConfig("ssl", false));
props.put("mail.smtp.ssl.protocols", "TLSv1.2 TLSv1.3");
props.put("mail.smtp.ssl.trust", channelConfig.getStringConfig("host"));
props.put("mail.smtp.connectiontimeout", "5000");
props.put("mail.smtp.timeout", "5000");
props.put("mail.smtp.writetimeout", "5000");
@@ -95,4 +101,26 @@ public class SmtpEmailChannelProvider implements EmailChannelProvider {
throw new IllegalArgumentException(message);
}
}
private void validateSecurityMode(ManagedChannelConfig channelConfig, int port) {
boolean starttls = channelConfig.getBooleanConfig("starttls", true);
boolean ssl = channelConfig.getBooleanConfig("ssl", false);
String providerCode = channelConfig.entity().getProviderCode();
String providerLabel = ChannelProviderCodeEnum.fromCode(providerCode).getLabel();
if (starttls && ssl) {
throw new IllegalArgumentException(providerLabel + " 配置不正确:STARTTLS 和 SSL 不能同时启用。"
+ " 如果使用 587 端口,请启用 STARTTLS 并关闭 SSL;如果使用 465 端口,请启用 SSL 并关闭 STARTTLS。");
}
if (port == 587 && ssl) {
throw new IllegalArgumentException(providerLabel + " 配置不正确:587 端口不应启用 SSL。"
+ " 请改成 STARTTLS=开启、SSL=关闭。");
}
if (port == 465 && starttls) {
throw new IllegalArgumentException(providerLabel + " 配置不正确:465 端口通常使用隐式 SSL。"
+ " 请改成 SSL=开启、STARTTLS=关闭。");
}
}
}
@@ -97,6 +97,16 @@
- 模板已绑定且匹配当前通道:允许发送
- 模板已绑定但不匹配当前通道:直接拒绝
### 11. SMTP 配置校验优化
- 邮件通道健康检查现在会先校验 SMTP 安全配置组合
- 明确拦截以下错误配置:
- `STARTTLS` 和 `SSL` 同时启用
- `587` 端口错误开启 `SSL`
- `465` 端口错误开启 `STARTTLS`
- 后台页面补充 Gmail / QQ 邮箱配置说明
- 健康检查失败时会优先返回中文可操作提示,降低排障成本
## 数据库 SQL
本轮新增 SQL 文件:
@@ -18,6 +18,7 @@ const PROVIDERS = {
EMAIL: {
GMAIL: {
label: "Gmail SMTP",
helpText: "Gmail 推荐使用 587 端口,开启 STARTTLS,关闭 SSL,并填写 Google 应用专用密码而不是邮箱登录密码。",
configFields: [
{ key: "host", label: "SMTP 主机", placeholder: "smtp.gmail.com", required: true, defaultValue: "smtp.gmail.com" },
{ key: "port", label: "SMTP 端口", placeholder: "587", required: true, defaultValue: "587" },
@@ -33,6 +34,7 @@ const PROVIDERS = {
},
QQMAIL: {
label: "QQ 邮箱 SMTP",
helpText: "QQ 邮箱常用 587 端口配合 STARTTLS,或 465 端口配合 SSL。密码请填写 SMTP 授权码。",
configFields: [
{ key: "host", label: "SMTP 主机", placeholder: "smtp.qq.com", required: true, defaultValue: "smtp.qq.com" },
{ key: "port", label: "SMTP 端口", placeholder: "587", required: true, defaultValue: "587" },
@@ -428,6 +430,11 @@ function renderDynamicFields() {
const schema = providerSchema(elements.channelType.value, elements.providerCode.value);
const config = state.editingChannel?.config || {};
const secretMask = state.editingChannel?.secretConfigMask || {};
const helperBlocks = [];
if (schema.helpText) {
helperBlocks.push(`<div class="hint-box compact-hint">${escapeHtml(schema.helpText)}</div>`);
}
const configFields = schema.configFields.map((field) => {
const value = config[field.key] ?? field.defaultValue ?? "";
@@ -474,7 +481,7 @@ function renderDynamicFields() {
`;
});
elements.dynamicFields.innerHTML = [...configFields, ...secretFields].join("");
elements.dynamicFields.innerHTML = [...helperBlocks, ...configFields, ...secretFields].join("");
}
function resetChannelForm(channel = null) {
@@ -134,6 +134,10 @@ code {
line-height: 1.6;
}
.compact-hint {
margin-bottom: 0;
}
.table-wrap {
overflow-x: auto;
}