Ubuntu Linode VPS配置Postfix通过Gmail中继发送邮件失败(连接smtp.google.com超时)
Ubuntu Linode VPS配置Postfix通过Gmail中继发送邮件失败(连接smtp.google.com超时)
看起来你已经完成了不少前置准备工作——配置MX记录、申请Linode开放SMTP端口、安装Postfix并做了基础配置,但卡在了Postfix无法连接Gmail SMTP服务器这一步,我帮你梳理几个关键的排查点和修复方案:
1. 核心问题:SMTP服务器域名错误
你的Postfix配置里relayhost = smtp.google.com:587是错误的!Gmail官方的SMTP服务器域名是smtp.gmail.com,不是smtp.google.com。这个域名错误会导致解析异常,直接引发连接超时。
修复步骤:
- 编辑Postfix主配置文件:
sudo nano /etc/postfix/main.cf - 修改
relayhost行:relayhost = smtp.gmail.com:587 - 保存后重载Postfix:
sudo systemctl reload postfix
2. 验证应用密码与SASL配置
虽然你提到用了应用密码,但再确认以下细节:
- 你的Gmail账号已经开启两步验证(应用密码必须基于开启2FA的账号才能生成)
- 生成的应用密码属于「邮件」类别,而非其他服务类型
- 检查
/etc/postfix/sasl_passwd文件内容是否符合格式:smtp.gmail.com:587 your-full-gmail-address@gmail.com:your-app-password - 确保执行过映射文件生成命令:
sudo postmap /etc/postfix/sasl_passwd sudo systemctl reload postfix
3. 直接测试SMTP连接
在VPS上用nc工具直接测试连接Gmail的SMTP端口,确认网络层面是否能连通:
nc -zv smtp.gmail.com 587
如果返回Connection to smtp.gmail.com 587 port [tcp/submission] succeeded!,说明网络没问题;如果还是超时,建议:
- 再次联系Linode确认SMTP端口(25/465/587)的限制确实已全局解除
- 自查VPS的IP是否被Gmail列入黑名单
4. 优化Postfix的TLS配置
Gmail的SMTP服务器强制要求TLS加密,你当前的smtp_tls_security_level=may可以改成更严格的encrypt,确保Postfix必须使用TLS连接:
smtp_tls_security_level=encrypt
修改后重载Postfix生效。
5. 重新测试邮件发送
完成以上修改后,用测试命令重新发送邮件:
echo "Test email content" | mail -s "Postfix Gmail Relay Test" your-personal-email@gmail.com
实时查看日志确认结果:
tail -f /var/log/mail.log
如果看到类似status=sent (250 2.0.0 OK ...)的日志,说明邮件发送成功。
备注:内容来源于stack exchange,提问作者LeperAffinity666
相关产品推荐
相关产品推荐

