3

I am trying to send Email from an asp.net webform using Gmail. the code works from my machine but when I upload the code to my windows server 2012 in Azure I get Unable to connect to Remote Server - exception

here is my code:

MailMessage mail = new MailMessage();
mail.Subject = "Subject";
mail.Body = "Main body goes here";

mail.From = new MailAddress("myAcount@gmail.com");

mail.IsBodyHtml = true;
mail.BodyEncoding = System.Text.Encoding.Unicode;
mail.SubjectEncoding = System.Text.Encoding.Unicode;

mail.To.Add("aaaa@gmail.com");

NetworkCredential cred = new NetworkCredential("myAccount@gmail.com", "myPwd"); 
SmtpClient smtp = new SmtpClient("smtp.gmail.com");
smtp.EnableSsl = true;

smtp.Credentials = cred;
smtp.Port = 587;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;

smtp.Send(mail);

Any ideas?

user822697
  • 49
  • 2
  • 3

1 Answers1

2

Make sure the local VM's Firewall rules are permitting outgoing connection via port 587. Here is a good article on how to create outbound Firewall rule.

astaykov
  • 30,768
  • 3
  • 70
  • 86