我正在尝试使用启用了双重身份验证的 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
答案 0 :(得分:-1)
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}.");