I was trying to send an email using smtp.gmail.com with below code (WPF - C#).
Can someone please let me know what is wrong here.
SmtpClient smtpServer = new SmtpClient("smtp.gmail.com");
smtpServer.Port = 587;
smtpServer.UseDefaultCredentials = false;
smtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpServer.Credentials = new System.Net.NetworkCredential("username", "passwd");
smtpServer.EnableSsl = true;
MailMessage msg = new MailMessage();
msg.To.Add("ToAddress");
msg.From = new MailAddress("FromAddress");
msg.Subject = "Test Mail";
msg.Body = "Test email from c#";
smtpServer.Send(msg);
It throws an exception:
Failure sending mail
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 173.194.66.108:587"}
System.Exception {System.Net.Sockets.SocketException}
I even tried changing port to 465 but didn't help. And also, I have turned off the security for Less secure app in configuration in gmail.