0

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
Niko
  • 251
  • 3
  • 6
  • 17
  • Did you see these two: http://stackoverflow.com/q/12692064/231316 and http://stackoverflow.com/q/10461257/231316 – Chris Haas Oct 28 '13 at 14:34
  • Yeah, but I think they are different. In their case it is their service that are blocking outgoing requests instead of Google's server blocking incoming requests... – Niko Oct 28 '13 at 14:40
  • This is a local Winsock error message, not remote, so Google isn't yet involved. Do you have access to firewall rules and/or AV? http://msdn.microsoft.com/en-us/library/windows/desktop/ms740668(v=vs.85).aspx#WSAEACCES – Chris Haas Oct 28 '13 at 16:26

1 Answers1

0

I had the same problem with Hostgator , and I tried switching between many email service providers. But HG was blocking their SMTP sockets.

Now I'm sending mails from MailGun using their REST API. Use any Transactional Email Service API. You'll get .Net clients for MailGun and Mandrill in NuGet. Or you can call their API using RestSharp

Ajeesh M
  • 2,092
  • 1
  • 14
  • 18