尝试通过Office 365发送电子邮件时SMTP 5.7.57错误

我正在尝试设置一些代码以通过Office 365经过身份validation的SMTP服务发送电子邮件:

var _mailServer = new SmtpClient(); _mailServer.UseDefaultCredentials = false; _mailServer.Credentials = new NetworkCredential("test.user@mydomain.com", "password"); _mailServer.Host = "smtp.office365.com"; _mailServer.TargetName = "STARTTLS/smtp.office365.com"; // same behaviour if this lien is removed _mailServer.Port = 587; _mailServer.EnableSsl = true; var eml = new MailMessage(); eml.Sender = new MailAddress("test.user@mydomain.com"); eml.From = eml.Sender; eml.to = new MailAddress("test.recipient@anotherdomain.com"); eml.Subject = "Test message"; eml.Body = "Test message body"; _mailServer.Send(eml); 

这似乎不起作用,我看到一个例外:

SMTP服务器需要安全连接或客户端未经过身份validation。 服务器响应是:5.7.57 SMTP; 客户端未通过身份validation,无法在MAIL FROM期间发送匿名邮件
在System.Net.Mail.MailCommand.Send(SmtpConnection conn,Byte []命令,字符串来自)
在System.Net.Mail.SmtpTransport.SendMail(MailAddress sender,MailAddressCollection recipients,String deliveryNotify,SmtpFailedRecipientException&exception)
在System.Net.Mail.SmtpClient.Send(MailMessage消息)

我已经尝试启用网络跟踪 ,似乎建立了安全通信(例如,我在日志中看到“STARTTLS”命令的一行,后来日志中有一行“远程证书被validation为有效user。“,以下Send()Receive()数据不能以纯文本forms读取,并且似乎不包含任何TLS / SSH恐慌)

我可以使用相同的电子邮件地址和密码登录http://portal.office.com/并使用Outlook电子邮件Web邮件发送和阅读电子邮件,因此在以编程方式发送电子邮件时可能导致身份validation失败的原因?

有没有办法额外调试加密流?

在我的情况下,在我没有运气的情况下尝试了所有这些建议之后,我联系了Microsoft支持,他们的建议只是更改密码。

这解决了我的问题。

请注意,密码未过期,因为我成功登录了office365,但重置解决了问题。

获得的经验:不信任Office 365密码到期日期,在我的情况下,密码将在1-2个月后过期,但它无法正常工作。 这导致我在我的代码中进行调查,并且仅在很长一段时间后才意识到问题出在Office365密码中“已损坏”或“过早过期”。

不要忘记每3个月“刷新”一次密码。

为了帮助调试,请尝试暂时切换到MailKit并使用以下代码段:

 using System; using MailKit.Net.Smtp; using MailKit.Security; using MailKit; using MimeKit; namespace TestClient { class Program { public static void Main (string[] args) { var message = new MimeMessage (); message.From.Add (new MailboxAddress ("", "test.user@mydomain.com")); message.To.Add (new MailboxAddress ("", "test.recipient@anotherdomain.com")); message.Subject = "Test message"; message.Body = new TextPart ("plain") { Text = "This is the message body." }; using (var client = new SmtpClient (new ProtocolLogger ("smtp.log"))) { client.Connect ("smtp.office365.com", 587, SecureSocketOptions.StartTls); client.Authenticate ("test.user@mydomain.com", "password"); client.Send (message); client.Disconnect (true); } } } } 

这会将整个事务记录到一个名为“smtp.log”的文件中,然后您可以阅读该文件并查看可能出错的位置。

请注意,smtp.log可能包含AUTH LOGIN命令,后跟一些base64编码的命令(这些是您的用户/通行证),因此如果您共享日志,请务必擦除这些行。

我希望这与您在System.Net.Mail中看到的错误相同,但它会帮助您查看正在发生的事情。

假设它失败了(我希望它会失败),尝试更改为SecureSocketOptions.None和/或尝试注释掉Authenticate()

看看它如何改变你所看到的错误。

请确保您使用该帐户的实际office365电子邮件地址。 您可以通过单击Outlook365中的配置文件按钮找到它。 我一直在进行身份validation,直到我意识到我尝试用于身份validation的电子邮件地址不是实际的邮箱电子邮件帐户。 实际的帐户电子邮件可能具有以下forms:account@company.onmicrosoft.com。

我们通过将邮箱(从地址)从“共享”转换为“常规”来完成我们的工作。 在此更改之前,我的应用程序在从Gmail迁移到Office 365时退出发送电子邮件。除了将主机设置为smtp.office365.com之外,不需要更改其他代码。

请检查下面我测试过的使用Exchange Online发送电子邮件的代码:

  MailMessage msg = new MailMessage(); msg.To.Add(new MailAddress("YourEmail@hotmail.com", "XXXX")); msg.From = new MailAddress("XXX@msdnofficedev.onmicrosoft.com", "XXX"); msg.Subject = "This is a Test Mail"; msg.Body = "This is a test message using Exchange OnLine"; msg.IsBodyHtml = true; SmtpClient client = new SmtpClient(); client.UseDefaultCredentials = false; client.Credentials = new System.Net.NetworkCredential("XXX@msdnofficedev.onmicrosoft.com", "YourPassword"); client.Port = 587; // You can use Port 25 if 587 is blocked client.Host = "smtp.office365.com"; client.DeliveryMethod = SmtpDeliveryMethod.Network; client.EnableSsl = true; try { client.Send(msg); } catch (Exception ex) { } 

在此处输入图像描述 端口(587)被定义用于消息提交。 尽管端口587并未强制要求STARTTLS,但端口587的使用在与客户端和服务器之间的通信的SSL / TLS加密是重要的安全和隐私问题的同时变得流行。

在我的情况下,我的问题与代码无关,而是与Exchange邮箱有关。 不知道为什么,但这解决了我的问题:

  • 转到该用户邮箱的Exchange设置并访问Mail Delegation
  • 在“ 代理发送”下,删除“ NT AUTHORITY \ SELF” ,然后添加用户的帐户。

这为用户授予代表他自己发送电子邮件的权限。 从理论上讲, NT AUTHORITY \ SELF应该做同样的事情,但由于某些原因不起作用。

资料来源: http : //edudotnet.blogspot.com.mt/2014/02/smtp-microsoft-office-365-net-smtp.html