我有以下发送电子邮件的代码。它在本地运行良好,但是在我的 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