当我运行localhost时,我有一个可以正常工作的SmtpClient。但是,在谷歌云服务器上,它返回:
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 xx.xx.xx.xx:587
我的代码:
MailMessage mailMessage = new MailMessage();
mailMessage.To.Add("User email");
mailMessage.From = new MailAddress("My Company Email");
mailMessage.Subject = "Test";
mailMessage.Body = "MSG Test";
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.mycompany.com"; //Or Your SMTP Server Address
smtp.Port = 587;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential
("email@mycompany.com", "password");
smtp.Send(mailMessage);
有谁知道为什么?我应该使用Azure代替google cloud for .net应用程序吗?
由于
答案 0 :(得分:2)
我怀疑问题是计算引擎默认阻止端口587上的流量,除了已知的IP地址。
当通过这些端口将流量发送到其外部IP地址时,Compute Engine会阻止或限制通过Internet和虚拟机之间以及两个虚拟机之间的所有以下端口/协议的流量(这还包括负载平衡地址) 。这些端口被永久阻止;它们无法使用防火墙规则打开。
- ...
- 除已知的Google IP地址外,大多数到端口465或587(SMTP over SSL)的传出流量都被阻止。
另外,来自"Sending Email from an Instance"文档:
Google Compute Engine不允许端口25,465和587上的出站连接。默认情况下,这些出站SMTP端口会被阻止,因为这些端口容易受到大量滥用。此外,拥有受信任的第三方提供商(如SendGrid,Mailgun或Mailjet)可以减轻计算引擎的负担,并使您无法保持接收方的IP信誉。
后一个文档页面提供了许多替代方法。