I have the following code that sends an email. It works fine locally but when deploying on my hostgator windows server I get the exception below.
I would guess that I need to whitelist my servers IP in gmail somwhow but how do I do that?
Code:
var fromAddress = new MailAddress("XXX@gmail.com", "From XXX");
var toAddress = new MailAddress("XXX@gmail.com", "To XXX");
const string fromPassword = "XXX";
const string subject = "Email Headline";
string body = "Email Body";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
})
{
smtp.Send(message);
}
Exception:
System.Net.Mail.SmtpException: 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.77.109:587