简体   繁体   中英

Whitelist IP gmail smtp - System.Net.Mail.SmtpException: Failure sending mail

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM