I'm trying to send an email using C# console but failing each time with error says:
Failure sending mail. ---> System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: An attempt was made to access a socket in a way forbidden by its access permissions 173.194.65.109:587
this is my C# code:
SmtpClient client = new SmtpClient();
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;
client.Host = "smtp.gmail.com";
client.Port = 587;
// setup Smtp authentication
System.Net.NetworkCredential credentials = new System.Net.NetworkCredential("myEmail@gmail.com", "myPassword");
client.UseDefaultCredentials = false;
client.Credentials = credentials;
MailMessage msg = new MailMessage();
msg.From = new MailAddress("myEmail@gmail.com");
msg.To.Add(new MailAddress("destinationEmail@gmail.com"));
msg.Subject = "This is a test Email subject";
msg.IsBodyHtml = true;
msg.Body = string.Format("<html><head></head><body><b>Test HTML Email</b></body>");
client.Send(msg);
I've also tried to use:
client.UseDefaultCredentials = true;
but still no success.
this is a generic C# code that is written all over the web, in each forum...but it does not help me.