Azure Function应用无法连接本地SMTP服务器的排查求助
问题:Azure Functions 使用 NodeMailer 连接私有网络 SMTP 服务器超时
错误日志
Result: Failure Exception: connect ETIMEDOUT [SMTP server IP address]:50025 Stack: Error: connect ETIMEDOUT [SMTP server IP address]:50025 at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1161:16)
相关代码片段
// Send the email const transporter = nodemailer.createTransport({ host: "[SMTP server IP address]", port: 50025, secure: false, ignoreTLS: true, connectionTimeout: 180000, debug: true }); let mailOptions = { .... } context.log('Sending email'); let info = await transporter.sendMail({mailOptions, function(error, info) { if (error) { context.log("sent mail error: " +error.message); } context.log(info.response); } });
当前排查情况
- 日志打印出
Sending email,但未触发回调内的错误或成功日志 - 已将Azure Function出站IP加入私有网络SMTP服务器白名单,但请求未到达防火墙
- 尝试调整NodeMailer配置(增加超时、启用TLS)无效
解决建议
1. 修正代码的异步调用错误
你当前混用了await和回调函数,transporter.sendMail的两种调用模式不能同时使用:
- 若用
await,函数会返回Promise,无需传入回调,错误需用try/catch捕获 - 若用回调模式,不会返回Promise,
await会无效,导致代码逻辑异常,无法捕获错误
正确写法二选一:
纯 await 模式
try { let info = await transporter.sendMail(mailOptions); context.log(info.response); } catch (error) { context.log("sent mail error: " + error.message); }
回调模式
transporter.sendMail(mailOptions, function(error, info) { if (error) { context.log("sent mail error: " + error.message); } context.log(info.response); });
2. 验证Azure Function的网络可达性
- 消费计划的Azure Function有多个出站IP,需将门户「网络」→「出站IP地址」中的所有IP加入白名单,不能只加单个
- 检查私有网络防火墙/安全组规则,确认放行Function出站IP到SMTP端口50025的TCP流量
- 添加简单的TCP连接测试代码,排查基础网络连通性:
const net = require('net'); const client = net.connect({ port: 50025, host: "[SMTP server IP address]" }, () => { context.log('Connected to SMTP server'); client.end(); }); client.on('error', (err) => { context.log('Connection error:', err.message); });
3. 检查SMTP服务器配置
- 确认SMTP服务器监听的是可外部访问的IP(如0.0.0.0),而非仅本地回环地址(127.0.0.1)
- 验证服务器防火墙是否允许来自Azure对应区域的流量,部分场景下动态出站IP可能未被覆盖
4. 尝试VNet集成方案
若消费计划的出站IP白名单无法覆盖,可将Function部署到专用计划或Premium计划,启用VNet集成,让Function直接接入私有网络,流量无需通过公共IP访问SMTP服务器,彻底解决白名单适配问题。
内容的提问来源于stack exchange,提问作者mdailey77
相关产品推荐
相关产品推荐

