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

使用Nodemailer结合EJS发送Gmail邮件时遭遇ECONNREFUSED连接错误求助

Troubleshooting ECONNREFUSED Error with Nodemailer & Gmail

Hey there, let's work through this SMTP connection issue step by step—this ECONNREFUSED error is super common with Gmail and Nodemailer, and we’ve got a few solid fixes to try:

1. Check Network & Firewall Blockages

That error means your machine can’t reach Gmail’s SMTP server on port 465. First, verify connectivity:

  • Run telnet smtp.gmail.com 465 in your terminal. If it fails, your local firewall, company network, or ISP is blocking this port.
  • Quick fix: Switch to port 587 (Gmail supports STARTTLS here instead of SSL). Update your transporter config like this:
    var transporter = nodemailer.createTransport({
        service: 'gmail',
        port: 587,
        secure: false, // Disable SSL for 587
        requireTLS: true, // Force STARTTLS encryption
        auth: {
            user: 'sample@gmail.com',
            pass: 'your-app-password' // We’ll talk about this next
        },
    });
    

2. Fix Gmail’s Authentication Requirements

Gmail stopped allowing regular account passwords for SMTP access back in 2022 (unless you have a legacy workspace account). You need one of these two options:

  • Enable 2-Step Verification + Use an App Password:
    1. Go to your Google Account > Security.
    2. Turn on 2-Step Verification if it’s off.
    3. Under "Signing in to Google", select "App passwords".
    4. Generate a 16-character app password for "Mail" and your device, then use this password in your pass field instead of your regular login password.
  • OAuth2 Authentication (Production-Grade):
    For apps going to production, OAuth2 is more secure. It lets users authenticate without sharing passwords—you can set this up by following Nodemailer’s official guidance for Gmail OAuth2 integration.

3. Simplify Your Transporter Config

When you set service: 'gmail', Nodemailer already has pre-configured settings for host, port, and security. Your manual overrides might be causing conflicts. Try stripping it down to this:

var transporter = nodemailer.createTransport({
    service: 'gmail',
    auth: {
        user: 'sample@gmail.com',
        pass: 'your-app-password'
    },
});

This uses Nodemailer’s tested, built-in Gmail settings, which avoids misconfigurations.

4. Double-Check Credentials

Make sure your user field is your full Gmail address (no typos!), and your pass is either the app password we mentioned earlier or (only if you absolutely must) a legacy password with "Less secure app access" enabled (not recommended for security reasons).


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.30 20:57:52