[英]System.Net.Sockets.SocketException on 2008R2 Server
我有一个要在服务器上运行的程序,该程序能够监视网络上的各种服务器,并在涉及某些服务器的某些情况下发送电子邮件通知。
目前,我可以在本地计算机上运行该程序,并启动电子邮件(使用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电子邮件路由器生成的电子邮件)。
是什么导致此错误?
该问题与Gmail阻止尝试发送电子邮件的服务器的IP地址有关。
尝试使用Gmail帐户发送电子邮件时,请先尝试使用浏览器在该IP地址上的特定计算机上手动登录Gmail。 我最终必须这样做,并经历验证从我的服务器发送的电子邮件发送尝试(以编程方式生成)是合法的过程。 如果您没有像我一样经历此过程,则Gmail可以假设您的服务器不是合法的电子邮件发件人,并且可以继续假设来自您IP地址的所有流量均无效-直到您如上所述正式验证该IP地址。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.