【问题标题】:System.Net.Sockets.SocketException on 2008R2 Server2008R2 服务器上的 System.Net.Sockets.SocketException
【发布时间】:2014-02-14 15:56:34
【问题描述】:

我想在服务器上运行一个程序,该程序能够监控网络上的各种服务器,并在涉及这些服务器的某些情况发生时发送电子邮件通知。

目前,我可以在我的本地机器上运行这个程序,并让它触发电子邮件(使用 gmail SMTP)。但是,当我尝试通过命令提示符在服务器上运行它时,它会引发以下异常:

Unhandled Exception: System.Net.Mail.SmtpException: Failure sending mail. --->
System.Net.WebException: Unable to connect to the remote server --->
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
xxx.xxx.xxx.xxx:587

与发送电子邮件相关的代码相当简单:

static void sendEmail(MailAddressCollection mailCollection, string emailSender,
                      string emailDisplayName, string emailPassword,
                      string subject, string body) {

    MailAddress fromAddress = new MailAddress(emailSender, emailDisplayName);

    SmtpClient smtpClient = new SmtpClient {
        Host = "smtp.gmail.com",
        Port = 587,
        EnableSsl = true,
        DeliveryMethod = SmtpDeliveryMethod.Network,
        UseDefaultCredentials = false,
        Credentials = new NetworkCredential(fromAddress.Address, emailPassword)
    };

    MailMessage message = new MailMessage {
        From = fromAddress,
        Subject = subject,
        Body = body
    };

    foreach (MailAddress mailAddress in mailCollection) {
        message.To.Add(mailAddress);
    }

    smtpClient.Send(message);

}

有问题的服务器是 2008R2 机器,它没有启用 Windows 防火墙。此外,端口 25 绝对是开放的,因为此服务器可以发送其他类型的电子邮件(特别是由 Dynamics CRM 电子邮件路由器生成的电子邮件)。

什么可能导致这个错误?

【问题讨论】:

  • “端口 25 肯定是打开的”但是脚本在端口 587 上使用 smtp.gmail.com。所以端口 587 是否打开?
  • 我不确定如何检查,但我尝试了“netstat -a”,但在列表中没有看到端口 587。不过我确实看到了 25 端口。
  • 在服务器上,尝试使用 telnet。 “telnet smtp.gmail.com 587”,看看是否可以连接。
  • 我做了 telnet 测试,也失败了:“连接到 smtp.gmail.com...无法打开与主机的连接,在端口 587:连接失败”。
  • 然后我猜出站端口 587 被某些防火墙阻止了。你在公司防火墙后面吗?您提到 Dynamic CRM 可以发送电子邮件,为什么不使用与 Dynamic CRM 相同的设置来发送电子邮件?

标签: c# smtp gmail


【解决方案1】:

问题与 Gmail 阻止尝试发送电子邮件的服务器的 IP 地址有关。

尝试使用 Gmail 帐户发送电子邮件时,首先尝试使用浏览器在该特定 IP 地址的特定计算机上手动登录 Gmail。我最终不得不这样做,并通过验证来自我的服务器的电子邮件发送尝试(以编程方式生成)是否合法的过程。如果您没有像我一样完成此过程,Gmail 可以假设您的服务器不是合法的电子邮件发件人,并且可以继续假设来自您的 IP 地址的所有流量都是无效的——直到您按上述方式正式验证该 IP 地址。

【讨论】:

    猜你喜欢
    • 2023-03-25
    • 2013-06-10
    • 1970-01-01
    • 1970-01-01
    • 2017-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-23
    相关资源
    最近更新 更多