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

Ubuntu服务器上Zoho Mail SMTP连接超时问题求助

Ubuntu服务器使用Zoho Mail SMTP发送邮件超时问题求助

我在Ubuntu服务器上使用Zoho Mail SMTP服务器发送邮件时遇到连接超时错误,但相同代码在Windows环境下可以正常执行。相关配置如下:

  • 使用邮箱地址:mycustom_Email@example.com
  • SMTP服务器:smtp.zoho.com
  • SMTP端口:587

运行的Python代码如下:

from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from fastapi import FastAPI, HTTPException
from email.header import Header
from email.utils import formataddr
import smtplib  # 原代码遗漏此导入,已补充

sender_name= " Account Security"
smtp_server = "smtp.zoho.com"
smtp_port = 587
smtp_username = "########"
smtp_password = "PASSWORD" 

def send_reset_email(email, token, fullname):
    try:
        msg = MIMEMultipart()
        msg["From"] = formataddr((str(Header(sender_name, 'utf-8')), smtp_username))
        msg["To"] = email
        msg["Subject"] = "Password Reset Request"

        body = f"Dear {fullname},\n\n"
        body += "We received a request to reset your password. To proceed, please click the link below:\n\n"
        body += f"http://#####.com/reset-password?token={token}\n\n"
        body += "If you did not request this password reset, you can safely ignore this email. Your account security is important to us.\n\n"
        body += "Thank you for using our service!\n\n"
  
        msg.attach(MIMEText(body, "plain"))

        with smtplib.SMTP(smtp_server, smtp_port) as server:
            server.starttls()
            server.login(smtp_username, smtp_password)
            server.sendmail(smtp_username, email, msg.as_string())

    except Exception as e:
        raise HTTPException(status_code=500, detail="Failed to send reset email")

def send_verification_email(email, token):
    try:
        msg = MIMEMultipart()
        msg["From"] = smtp_username
        msg["To"] = email
        msg["Subject"] = "Email Verification Request"

        body = f"Dear User,\n\n"
        body += "We received a request to verify your email. To proceed, please click the link below:\n\n"
        body += f"https://########/verify_email/{token}\n\n"
        body += "If you did not request this email verification, you can safely ignore this email. Your account security is important to us.\n\n"
        body += "Thank you for using our service!\n\n"
    
        msg.attach(MIMEText(body, "plain"))

        with smtplib.SMTP(smtp_server, smtp_port) as server:
            server.starttls()
            server.login(smtp_username, smtp_password)
            server.sendmail(smtp_username, email, msg.as_string())
        print("Verification email sent successfully")
    except Exception as e:
        raise HTTPException(status_code=500, detail="Failed to send verification email")
    
print(send_verification_email("info@####.com","123456"))

排查与解决步骤

1. 测试网络连通性

先确认服务器能否访问Zoho的SMTP端口:

  • 执行telnet测试:telnet smtp.zoho.com 587
  • 或使用nc命令:nc -zv smtp.zoho.com 587
    如果连接失败,说明服务器防火墙/安全组限制了587端口的出站流量,需要开放。

2. 调整防火墙规则

  • Ubuntu默认使用ufw,开放587端口:sudo ufw allow out 587/tcp
  • 若使用云服务器,需同步在云平台安全组中添加出站方向的TCP 587端口允许规则。

3. 增加SMTP连接超时

在代码中手动设置更长的超时时间,避免默认超时过短:

with smtplib.SMTP(smtp_server, smtp_port, timeout=30) as server:
    server.starttls()
    # 后续代码保持不变

4. 检查Zoho账户设置

  • 确认Zoho邮箱已启用SMTP服务(进入邮箱设置→"Mail Accounts"→"Outgoing Server (SMTP)"开启)
  • 若账户开启了两步验证,需使用应用专用密码代替原账户密码登录SMTP。

5. 验证DNS解析

确认服务器能正确解析Zoho的SMTP域名:
执行nslookup smtp.zoho.com,若解析异常,可更换DNS服务器(如改为8.8.8.8)。

6. 尝试SSL直连

若587端口的STARTTLS方式仍有问题,可改用465端口的SSL连接:
修改代码中的SMTP类为SMTP_SSL,端口改为465:

with smtplib.SMTP_SSL(smtp_server, 465) as server:
    server.login(smtp_username, smtp_password)
    # 后续代码保持不变

同时确保服务器开放465端口的出站流量。

内容的提问来源于stack exchange,提问作者Yahya Al Hattali

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.08 14:10:57