2013-08-22 48 views
0

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

目前,我可以在我的本地机器上运行此程序,并让它启动电子邮件(使用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防火墙。此外,由于此服务器可以发送其他类型的电子邮件(特别是那些由Dynamics CRM电子邮件路由器生成的电子邮件),因此端口25肯定是打开的。

这可能是造成这个错误?

+0

“端口25绝对开放”,但脚本在端口587上使用smtp.gmail.com。端口587是否打开? –

+0

我不知道如何检查,但我尝试了“netstat -a”,但没有看到列表中的端口587。我确实看到了25号港口。 – nairware

+0

在服务器上,尝试使用telnet。 “telnet smtp.gmail.com 587”并查看是否可以连接。 –

回答

0

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

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