白名单IP gmail smtp - System.Net.Mail.SmtpException:发送邮件失败

时间:2013-10-28 14:20:40

标签: c# email smtpclient

我有以下代码发送电子邮件。它在本地工作正常,但在我的hostgator Windows服务器上部署时,我得到以下例外。

我猜我需要在gmail somwhow中将我的服务器IP列入白名单,但我该怎么做?

代码:

            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);
            }

例外:

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

1 个答案:

答案 0 :(得分:0)

我和Hostgator有同样的问题,我尝试在许多电子邮件服务提供商之间切换。但是HG阻止了他们的SMTP套接字。

现在我正在使用他们的REST API从MailGun发送邮件。 使用任何交易电子邮件服务API 。您将在NuGet中获得MailGunMandrill的.Net客户端。或者,您可以使用RestSharp

调用其API