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

本地正常的邮件发送API部署服务器后报错求助

邮件发送API本地正常,部署到服务器后报错

问题描述

开发的邮件发送API在本地环境可正常发送邮件,但部署到服务器后出现发送失败的报错。已确认代码无问题,尝试多种方法仍未解决。

报错信息:

System.Net.Mail.SmtpException: Failure sending mail. ---> System.Net.WebException: The operation has timed out. ---> System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond x.x.x.x:587

相关代码:

SmtpClient SmtpMail = new SmtpClient();
SmtpMail.Host = "hostname";
SmtpMail.Port = 587;
SmtpMail.Credentials = new NetworkCredential("mailid", "password");
SmtpMail.EnableSsl = false;
SmtpMail.ServicePoint.MaxIdleTime = 0;
SmtpMail.ServicePoint.SetTcpKeepAlive(true, 2000, 2000);
SmtpMail.Send(mail);

排查方向与解决方法

  • 检查服务器网络限制
    云服务器或企业内网服务器通常会限制出站端口,确认服务器的安全组、防火墙是否开放了587端口的出站权限;如果是内网环境,联系运维确认网关是否拦截了SMTP请求,需将目标SMTP服务器添加到白名单。

  • 验证SMTP服务商的IP限制
    多数邮件服务商(如腾讯企业邮、Gmail等)会限制陌生IP的SMTP连接,登录邮箱后台将服务器公网IP加入IP白名单;同时确认代码中使用的是邮箱授权码而非登录密码,部分邮箱要求必须用授权码登录SMTP。

  • 修正EnableSsl配置
    587端口一般强制要求STARTTLS加密,把代码里的SmtpMail.EnableSsl = false改为true,再测试发送。

  • 测试服务器与SMTP服务器的连通性
    在服务器上执行telnet hostname 587或者PowerShell命令Test-NetConnection hostname -Port 587,如果无法连接,优先排查网络路由、防火墙问题。

  • 增加超时时间
    默认SmtpClient超时较短,添加SmtpMail.Timeout = 10000;(设置为10秒),避免因网络延迟导致超时。

  • 确认邮箱服务状态
    登录邮箱后台,检查是否手动开启了POP3/SMTP服务,部分邮箱默认关闭该功能,需手动启用。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 22:55:18