我正在尝试使用已启用双因素身份验证的office365电子邮件帐户发送电子邮件。它给出了身份验证失败的错误。对于尚未启用双因素身份验证的电子邮件帐户,效果很好。如何解决这个问题?
using (SmtpClient client = new SmtpClient())
{
client.Port = Convert.ToInt32(appSettings["Port"]);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Host = "smtp.office365.com";
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(SenderMailAddress, SenderMailPassword);
email.Subject = String.Format("{0}", txtMailSubject.Text);
//
email.Body = String.Format("{0}", text);
email.IsBodyHtml = true;
client.Send(email);
}
错误信息是
System.Net.Mail.SmtpException:SMTP 服务器需要安全的 连接或客户端未经过身份验证。服务器响应 是:5.7.1 客户端未经过身份验证 System.Net.Mail.MailCommand.CheckResponse
您必须创建一个应用程序密码,然后在代码中使用新的应用程序密码,请检查此链接
MailAddress from = new MailAddress("fromid");
MailAddress to = new MailAddress("toID");
MailMessage message = new MailMessage(from, to);
message.Subject = "Using the new SMTP client.";
message.Body = @"The sensor get offline ...";
SmtpClient client = new SmtpClient(server);
client.Host = "hostID";
client.Port = 587;
client.EnableSsl = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = true;
client.Credentials = new NetworkCredential
{
UserName = "**********",
Password = "*************,
};
Console.WriteLine("Sending an email message to {0} using the SMTPhost {1}.");