修复:优化消息中心 SMTP 校验提示
This commit is contained in:
+29
-1
@@ -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;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
# 2026-03-23 MessageHub Gmail 体检提示优化
|
||||
|
||||
## 现象
|
||||
|
||||
- 后台配置 Gmail SMTP 后,执行“体检”返回:
|
||||
- `SMTP 连接失败: Exception reading response`
|
||||
|
||||
## 原因定位
|
||||
|
||||
- 这类报错在 JavaMail 里通常过于底层,不够直观。
|
||||
- 本次排查确认,最常见原因是 SMTP 安全配置组合不正确。
|
||||
- 你当前截图中的组合是:
|
||||
- `端口 = 587`
|
||||
- `启用 STARTTLS = 是`
|
||||
- `启用 SSL = 是`
|
||||
- 其中 `587 + STARTTLS + SSL 同时开启` 对 Gmail 来说是不合理的,容易直接触发连接握手异常。
|
||||
|
||||
## 正确配置建议
|
||||
|
||||
### Gmail 常规推荐
|
||||
|
||||
- `SMTP 主机`: `smtp.gmail.com`
|
||||
- `SMTP 端口`: `587`
|
||||
- `启用认证`: 开启
|
||||
- `启用 STARTTLS`: 开启
|
||||
- `启用 SSL`: 关闭
|
||||
- `SMTP 用户名`: 你的 Gmail 地址
|
||||
- `发件地址`: 你的 Gmail 地址
|
||||
- `SMTP 密码/授权码`: 使用 Google 应用专用密码,不要填邮箱登录密码
|
||||
|
||||
### Gmail 另一种常见组合
|
||||
|
||||
- `SMTP 端口`: `465`
|
||||
- `启用 SSL`: 开启
|
||||
- `启用 STARTTLS`: 关闭
|
||||
|
||||
## 本轮代码调整
|
||||
|
||||
- [SmtpEmailChannelProvider.java](/E:/code/xiaowang/MessageHub/src/main/java/com/xiaowang/messagehub/service/channel/email/SmtpEmailChannelProvider.java)
|
||||
- 健康检查前先执行 SMTP 配置校验
|
||||
- 明确拦截以下错误组合并返回中文提示:
|
||||
- `STARTTLS` 和 `SSL` 同时开启
|
||||
- `587` 端口开启 `SSL`
|
||||
- `465` 端口开启 `STARTTLS`
|
||||
- 补充 TLS 协议和 `mail.smtp.ssl.trust` 配置,提升兼容性
|
||||
- [app.js](/E:/code/xiaowang/MessageHub/src/main/resources/static/messagehub-admin/app.js)
|
||||
- Gmail / QQ 邮件通道表单新增配置说明
|
||||
- [styles.css](/E:/code/xiaowang/MessageHub/src/main/resources/static/messagehub-admin/styles.css)
|
||||
- 补充说明样式
|
||||
|
||||
## 处理建议
|
||||
|
||||
你现在先把 Gmail 通道改成下面这个组合再点一次体检:
|
||||
|
||||
- 端口:`587`
|
||||
- 认证:开启
|
||||
- STARTTLS:开启
|
||||
- SSL:关闭
|
||||
- 密码:Google 应用专用密码
|
||||
|
||||
如果还是失败,再继续排查:
|
||||
|
||||
- Google 账号是否已开启两步验证
|
||||
- 是否生成并使用了应用专用密码
|
||||
- 当前网络环境是否限制到 `smtp.gmail.com:587` 的外连
|
||||
|
||||
## 验证
|
||||
|
||||
- `MessageHub` 已重新通过 `mvn -q -DskipTests compile`
|
||||
- 后台脚本已通过 `node --check`
|
||||
Reference in New Issue
Block a user