尽管 Stackoverflow 上有关于此的所有主题。我无法找到这个问题的解决方案:
我有这个例外:
SMTP 服务器需要安全连接或客户端未通过身份验证。来自服务器的回复是:5.5.1 Authentication Required。了解更多信息
使用此代码:
var fromAddress = new MailAddress(user.Email, "From Name");
var toAddress = new MailAddress("myadress@gmail.com", "To Name");
const string fromPassword = "fromPassword";
const string subject = "Subject";
const string body = "Body";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword),
Timeout = 20000
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
})
{
smtp.Send(message);
}
请问我错过了什么?感谢您的帮助