我正在尝试发送电子邮件,但收到以下错误:
尝试以其访问权限禁止的方式访问套接字72.52.4.90:587
代码:
var mail = new MailMessage();
var client = new SmtpClient();
client.Port = 587;
client.Host = "smtp.gmail.com";
client.EnableSsl = true;
client.Timeout = 10000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("myemail@gmail.com", "password");
mail.From = new MailAddress("myemail@gmail.com");
mail.To.Add(new MailAddress("myemail@gmail.com"));
mail.BodyEncoding = UTF8Encoding.UTF8;
mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
mail.Subject = "this is a test email.";
mail.Body = "this is my test email body";
client.Send(mail);
注意:此代码基于StackOverflow中已有的其他问题和答案。然而,经过广泛搜索后,似乎到目前为止还没有其他人遇到过这个问题。根据另一个Q& A,此代码应该正常工作。
更新:在被告知这是重复之后,我已将代码修改为以下内容,但是,我仍然遇到同样的错误。
private static void Main(string[] args)
{
var From = new MailAddress("myemail@gmail.com", "Me");
var To = new MailAddress("myemail@gmail.com", "Me Too");
var client = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587, //25
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(From.Address, "password"),
Timeout = 20000
};
using (var mail = new MailMessage(From, To)
{
Subject = @"this is a test email.",
Body = @"this is my test email body"
})
{
client.Send(mail);
};
}