我有以下代码发送电子邮件。它在本地工作正常,但在我的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