Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot send mail from an Azure VM using Gmail

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("[email protected]");

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

mail.To.Add("[email protected]");

NetworkCredential cred = new NetworkCredential("[email protected]", "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?

like image 817
user822697 Avatar asked Nov 26 '25 05:11

user822697


1 Answers

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.

like image 151
astaykov Avatar answered Nov 28 '25 00:11

astaykov