You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ubuntu服务器上Nodemailer无法发送邮件的问题排查与解决

Nodemailer 连接 Gmail SMTP 超时问题解决(Ubuntu 22.04 服务器)

问题场景

本地 Win11 环境下,基于 Node.js + Nodemailer 的邮件发送程序运行正常,但部署到 Ubuntu 22.04 服务器后无法发送邮件,日志提示连接超时。

核心代码

var syncSql = require('sync-mysql');             
var nodemailer = require('nodemailer');   

var syncCon = new syncSql({ 
    host:'localhost', 
    user:'_nodeuser', 
    password:'_password', 
    database:'mydatabase'
});

var transporter = nodemailer.createTransport({
    service: 'gmail',
    auth: {
        user: '_email@gmail.com',
        pass: '_gmailAppPass'
    },
    logger: true
});

// 省略数据库查询逻辑
resp2 = syncCon.query(sql);

var mailOptions = {
    from: '_email@gmail.com',
    to: resp2.email,                       
    subject:'_email subject',     
    html: '_email body'
};

transporter.sendMail(mailOptions, function(error, info) {
    if(error) {
        console.log('remind_old_action(): '+ error);
    } else {
        console.log('Email sent: ' + info.response);   
    }
});

本地成功运行日志

[2023-03-20 11:33:08] DEBUG Creating transport: nodemailer (6.9.0; +https://nodemailer.com/; SMTP/6.9.0[client:6.9.0])
[2023-03-20 11:33:09] DEBUG Sending mail using SMTP/6.9.0[client:6.9.0]
[2023-03-20 11:33:10] DEBUG [0FD44uxJ0] Resolved smtp.gmail.com as 173.194.76.108 [cache miss]
[2023-03-20 11:33:10] INFO  [0FD44uxJ0] Secure connection established to 173.194.76.108:465
[2023-03-20 11:33:11] DEBUG [0FD44uxJ0] SMTP handshake finished
[2023-03-20 11:33:11] INFO  [0FD44uxJ0] User "_email@gmail.com" authenticated
[2023-03-20 11:33:11] INFO  Sending message <c25b179b-d610-4a55-8cdd-d5605ac0ae00@gmail.com> to <_target_email>
[2023-03-20 11:33:12] INFO  [0FD44uxJ0] <501 bytes encoded mime message (source size 498 bytes)>
[2023-03-20 11:33:13] DEBUG [0FD44uxJ0] Closing connection to the server using "end"
Email sent: 250 2.0.0 OK  1679311993 v15-20020a05600c444f00b003edddce5b00sm4292772wmn.12 - gsmtp
[2023-03-20 11:33:13] INFO  [0FD44uxJ0] Connection closed

服务器超时日志

[2023-03-20 11:40:27] DEBUG Creating transport: nodemailer (6.9.1; +https://nodemailer.com/; SMTP/6.9.1[client:6.9.1])
[2023-03-20 11:40:27] DEBUG Sending mail using SMTP/6.9.1[client:6.9.1]
[2023-03-20 11:40:27] DEBUG [jZWnAXUE78U] Resolved smtp.gmail.com as 172.253.63.108 [cache miss]
[2023-03-20 11:42:27] ERROR [jZWnAXUE78U] Connection timeout
[2023-03-20 11:42:27] DEBUG [jZWnAXUE78U] Closing connection to the server using "destroy"
[2023-03-20 11:42:27] ERROR Send Error: Connection timeout
remind_old_action(): Error: Connection timeout

排查过程

  • 排除 Nodemailer 本身问题:将代码迁移到 Express 服务中运行,本地正常但服务器依旧超时。
  • 排除反向代理影响:调整 Nginx 配置甚至直接关闭 Nginx,问题未得到解决。
  • 验证账号配置:已为谷歌账号开启 2FA 并生成 16 位应用专用密码,本地验证有效,服务器端配置与本地完全一致。

最终解决方案

问题根源是云服务商 Hetzner 封禁了 25/465 端口,导致 Nodemailer 无法建立与 smtp.gmail.com 的连接。


内容的提问来源于stack exchange,提问作者Dimitri

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 00:03:10