我有一个程序,我想在服务器上运行该程序,该程序能够监视网络上的各种服务器,并在发生涉及这些服务器的某些情况时发送电子邮件通知。
目前,我可以在我的本地机器上运行这个程序,并让它发送电子邮件(使用 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 电子邮件路由器生成的电子邮件)。
什么可能导致此错误?