0

I have an SmtpClient that works fine when I run it localhost. However, on google cloud server it returns:

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

My code:

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

Does anyone know why? Should I use Azure instead of google cloud for .net applications?

Thanks

Nick.Mc
  • 18,304
  • 6
  • 61
  • 91
user6824563
  • 735
  • 6
  • 25
  • 47
  • 1
    By default, public SMTP servers have very restrictive rules to stop spam.Are you certain that you have for example, whitelisted your source IP address? I just tested it with telnet and it responded OK. – Nick.Mc Nov 03 '17 at 01:17
  • 1
    I'll edit your question and blot out the IP address. – Nick.Mc Nov 03 '17 at 01:18
  • try port 25 on live instead of 587 – Zoinky Nov 03 '17 at 05:18
  • Try the code from your home computer? Are you able to connect to your company’s smtp server from outside the firewall to send email? A lot of company’s smtp is configure to just receive email from other client. Not to actually route email it receives from the outside. – Black Frog Nov 03 '17 at 06:59

1 Answers1

4

I suspect the problem is that traffic on port 587 is blocked by default on Compute Engine, except for known IP addresses.

From the firewall documentation:

Compute Engine blocks or restricts traffic through all of the following ports/protocols between the Internet and virtual machines, and between two virtual machines when traffic is addressed to their external IP addresses through these ports (this also includes load-balanced addresses). These ports are permanently blocked; they cannot be opened using firewall rules.

  • ...
  • Most outgoing traffic to port 465 or 587 (SMTP over SSL) is blocked, except for known Google IP addresses.

Also, from the "Sending Email from an Instance" documentation:

Google Compute Engine does not allow outbound connections on ports 25, 465, and 587. By default, these outbound SMTP ports are blocked because of the large amount of abuse these ports are susceptible to. In addition, having a trusted third-party provider such as SendGrid, Mailgun, or Mailjet relieves Compute Engine and you from maintaining IP reputation with your receivers.

This latter documentation page provides a number of alternative approaches.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194