[英]The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.57 SMTP; Office 365 error
[英]The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.57 SMTP;
当我尝试通过我的 C# 代码发送邮件时,上面显示错误。 我已经搜索了可能所有的解决方案,但我没有得到解决方案。 我正在使用以下代码:
MailMessage mail = new MailMessage("SenderMail",Email);
mail.IsBodyHtml = true;
mail.Subject = "An email from Office365";
mail.Body = "<html><body><h1>Hello world</h1></body></html>";
SmtpClient client = new SmtpClient("smtp.office365.com");
client.Port = 587;
client.EnableSsl = true;
client.UseDefaultCredentials = false; // Important: This line of code must be executed before setting the NetworkCredentials object, otherwise the setting will be reset (a bug in .NET)
NetworkCredential cred = new System.Net.NetworkCredential("Sendermail", "Password");
client.Credentials = cred;
client.Send(mail);
1- 我相信您的代码中缺少以下内容。
client.DeliveryMethod = SmtpDeliveryMethod.Network;
2- 不要将 UseDefaultCredentials 值设置为 false。 保持它的默认值。 只需删除该行。
这对我有用。
如果您还没有,那么我相信您可能需要启用从您的 Office365 帐户发送的客户端。 有关详细信息,请参阅此页面。
上下文:我遇到了同样的错误,我认为我的凭据很好,因为它每天都运行良好。 我试图调试,在互联网上寻找解决方案(即使在这个 stackoverflow 问题线程中),我花了很多时间没有得到好的结果。
解决方案:最后,我在Outlook Web 客户端上尝试了“假定正确”的凭据并登录。结果:我收到此消息:“您的密码已过期! ”然后我在 Web 客户端中更新了密码,更新了凭据和代码,就是这样它,它像往常一样工作正常。
附加信息:您可以考虑将密码(加密)保存在其他地方(不在源代码中),以便可以轻松更新。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.