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

如何解决Nodemailer结合Gmail/SMTP的生产环境邮件发送问题?

Hey there, let's tackle your email delivery issues one by one and get them sorted out!

1. Fixing Gmail SMTP Authentication for Overseas Production Servers

The timeout-based 2FA token setup Google requires is indeed a hassle—luckily, there's a better way using App Passwords (designed specifically for apps that don't support modern 2FA prompts):

  • First, make sure your Google account has 2FA enabled (this is a prerequisite for App Passwords)
  • Go to your Google Account > Security > App Passwords
  • Generate a new app password: select "Mail" as the app, choose "Other" as the device, and name it something like "Nodemailer Production"
  • Replace your current pass value in the code with this generated App Password
  • Also, fix your transporter config for production (disable the unsafe TLS bypass and use proper SSL):
const nodemailer = require('nodemailer');

// No need for separate nodemailer-smtp-transport—Nodemailer has this built-in now
const transporter = nodemailer.createTransport({
  service: 'gmail',
  host: 'smtp.gmail.com',
  port: 465,
  secure: true, // Use SSL for production
  auth: {
    user: 'info@gmail.com',
    pass: 'your-generated-app-password', // Replace with your App Password
  },
});

Note: Google has removed the "Less secure app access" option, so App Passwords are the only supported way to use Gmail SMTP with 2FA-enabled accounts.

2. Fixing SendGrid Emails Not Reaching Gmail

If SendGrid says emails are sent but Gmail isn't receiving them, it's almost always a domain authentication issue:

  • Set up SPF record: Add a TXT record to your domain's DNS: v=spf1 include:sendgrid.net ~all
  • Configure DKIM: Generate a DKIM key in your SendGrid dashboard, then add the corresponding TXT record to your domain DNS
  • Add DMARC record: Start with a monitoring record to avoid accidental blocks: v=DMARC1; p=none; sp=none; rua=mailto:dmarc@yourdomain.com (you can tighten this later to p=quarantine or p=reject once things work)
  • Verify your sender domain: In SendGrid, go to Sender Authentication and verify your samesoft.network domain—this tells Gmail the email is legitimately from you
  • Check SendGrid logs: Look for "dropped" or "bounced" statuses—if Gmail is marking emails as spam, tweak your content (avoid overly salesy language like "Congratulations!" and make sure the email body is relevant to your recipients)

3. Fixing Local SMTP "Sent but Not Received" Problem

If your local SMTP server logs say emails are sent but you don't get them, try these checks:

  • Relay configuration: Most local SMTP servers (like Postfix) need a relay setup to send external emails—without it, emails just sit in the local queue instead of being forwarded
  • ISP blocks: Many ISPs block outbound port 25 (default SMTP port). Try using port 587 (STARTTLS) or 465 (SSL) instead
  • Check server logs: Look for errors in your local SMTP logs (e.g., /var/log/mail.log on Linux) — common issues include "relay access denied" or "connection refused" when trying to reach Gmail's SMTP servers
  • Test with a tool: Use swaks or telnet to manually test your local SMTP:
    swaks --to your-gmail-address@gmail.com --from no-reply@samesoft.network --server localhost:587
    
    This will show you exactly where the delivery is failing.

Quick Code Tweaks

  • You don't need to install nodemailer-smtp-transport anymore—Nodemailer includes SMTP support out of the box
  • Make sure your OWNER_EMAIL variable is correctly defined and points to a valid Gmail address
  • For better deliverability, match your from address to your authenticated domain (e.g., use info@samesoft.network instead of no-reply@samesoft.network if you've verified samesoft.network in your email service)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.12 05:13:02