简体   繁体   English

2008R2服务器上的System.Net.Sockets.SocketException

[英]System.Net.Sockets.SocketException on 2008R2 Server

I have a program that I want to run on a server which is able to monitor various servers on a network and send out email notifications when certain situations involving those servers occur. 我有一个要在服务器上运行的程序,该程序能够监视网络上的各种服务器,并在涉及某些服务器的某些情况下发送电子邮件通知。

Currently, I am able to run this program on my local machine, and have it fire off emails (using gmail SMTP). 目前,我可以在本地计算机上运行该程序,并启动电子邮件(使用gmail SMTP)。 However, when I try to run it on the server via the command prompt, it throws the following exception: 但是,当我尝试通过命令提示符在服务器上运行它时,它将引发以下异常:

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

The code related to the sending of the email is fairly simple: 与发送电子邮件相关的代码非常简单:

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);

}

The server in question is a 2008R2 box, and it does not have Windows Firewall enabled. 有问题的服务器是2008R2的服务器,没有启用Windows防火墙。 Also, port 25 is definitely open, as this server can send other types of emails (specifically, those generated by a Dynamics CRM email router). 另外,端口25绝对是开放的,因为该服务器可以发送其他类型的电子邮件(特别是由Dynamics CRM电子邮件路由器生成的电子邮件)。

What could be causing this error? 是什么导致此错误?

The issue was related to Gmail blocking the IP address of the server attempting to send email. 该问题与Gmail阻止尝试发送电子邮件的服务器的IP地址有关。

When attempting to send emails using a Gmail account, first attempt logging into Gmail manually on that machine specific at that specific IP address, using a browser. 尝试使用Gmail帐户发送电子邮件时,请先尝试使用浏览器在该IP地址上的特定计算机上手动登录Gmail。 I ultimately had to do this and go through the process of authenticating that the email send attempts from my server (being generated programmatically) were legitimate. 我最终必须这样做,并经历验证从我的服务器发送的电子邮件发送尝试(以编程方式生成)是合法的过程。 If you do not go through this process like I did, Gmail can assume that your server is not a legitimate email sender and can continue to assume all traffic from your IP address is invalid--until you formally validate that IP address as described above. 如果您没有像我一样经历此过程,则Gmail可以假设您的服务器不是合法的电子邮件发件人,并且可以继续假设来自您IP地址的所有流量均无效-直到您如上所述正式验证该IP地址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.Net.Sockets.SocketException - System.Net.Sockets.SocketException postgres 连接上的 System.Net.Sockets.SocketException - System.Net.Sockets.SocketException on postgres connection System.Net.Sockets.SocketException 灾难 - System.Net.Sockets.SocketException Disaster .NET服务System.Net.Sockets.SocketException:'未知此类主机' - .NET Service System.Net.Sockets.SocketException: 'No such host is known' Lidgren 第二次创建服务器实例导致 System.Net.Sockets.SocketException - Lidgren create server instance for a second time results in System.Net.Sockets.SocketException System.Net.Sockets.SocketException (0x80004005): 没有这样的主机是已知的 - System.Net.Sockets.SocketException (0x80004005): No such host is known EventHub 发送消息,但 Application Insights 上的 System.Net.Sockets.SocketException - EventHub send message, but System.Net.Sockets.SocketException on Application Insights 使用Azure时Visual Studio中的System.Net.Sockets.SocketException - System.Net.Sockets.SocketException in Visual Studio while using Azure System.Net.Sockets.SocketException:'提供了无效的参数' - System.Net.Sockets.SocketException: 'An invalid argument was supplied' System.Net.Sockets.SocketException(0x80004005) - System.Net.Sockets.SocketException (0x80004005)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM