我正在尝试通过Gmail发送电子邮件,但此代码无效,导致连接超时错误。如果我制作端口' 587'它给出了这个错误:
SMTP服务器需要安全连接,或者客户端未经过身份验证。服务器响应为:5.5.1需要身份验证。
了解更多信息
string email = // email
string password = // password
string smtp = // smtp.gmail.com
int port = // 465
var from = new MailAddress(email, "");
var to = new MailAddress(message.Destination);
var client = new SmtpClient()
{
Host = smtp,
Port = port,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(email, password)
};
var mail = new MailMessage(from, to)
{
Subject = subject,
Body = body,
IsBodyHtml = true
};
return client.SendMail(mail);
}
答案 0 :(得分:0)
我过去使用的代码与你的代码非常相似而且有用,但我使用的端口号是587。 这是我的代码:
SmtpClient client = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential("gmail_login", "gmail_password")
};
using( var message = new MailMessage("your_emailAddress", "destination_Email")
{
Subject ="subject",
Body = "body"
})
client.Send(message);
如果您要从新目的地/时区登录,则应首先通过Web浏览器登录并确认它是您:)
答案 1 :(得分:0)