问题描述
我正在尝试设置一些代码以通过 Office 发送电子邮件365认证的SMTP服务:
I'm trying to set up some code to send email via Office 365's authenticated SMTP service:
var _mailServer = new SmtpClient();
_mailServer.UseDefaultCredentials = false;
_mailServer.Credentials = new NetworkCredential("[email protected]", "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("[email protected]");
eml.From = eml.Sender;
eml.to = new MailAddress("[email protected]");
eml.Subject = "Test message";
eml.Body = "Test message body";
_mailServer.Send(eml);
这似乎不起作用,我看到了一个例外:
This doesn't appear to be working, and I'm seeing an exception:
SMTP 服务器需要安全连接或客户端未通过身份验证.服务器响应为:5.7.57 SMTP;在 MAIL FROM
期间,客户端未经过身份验证以发送匿名邮件在 System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, String from)
在 System.Net.Mail.SmtpTransport.SendMail(MailAddress 发件人、MailAddressCollection 收件人、String deliveryNotify、SmtpFailedRecipientException& 异常)
在 System.Net.Mail.SmtpClient.Send(MailMessage message)
我尝试过启用网络跟踪 并且似乎建立了安全通信(例如,我在日志中看到一行STARTTLS"命令,后来在日志中看到一行远程证书已被用户验证为有效.",并且以下 Send()
和 Receive()
数据不能作为纯文本读取,并且似乎不包含任何 TLS/SSH 恐慌)
I've tried enabling network tracing and it appears that secure communications are established (for example, I see a line in the log for the "STARTTLS" command, and later there's a line in the log "Remote certificate was verified as valid by the user.", and the following Send()
and Receive()
data is not readable as plain text, and doesn't appear to contain any TLS/SSH panics)
我可以使用完全相同的电子邮件地址和密码登录到 http://portal.office.com/ 并使用 Outlook 电子邮件网络邮件发送和阅读电子邮件,那么以编程方式发送电子邮件时可能导致身份验证失败的原因是什么?
I can use the very same email address and password to log on to http://portal.office.com/ and use the Outlook email web mail to send and read email, so what might be causing the authentication to fail when sending email programmatically?
有没有办法额外调试加密流?
Is there any way to additionally debug the encrypted stream?
推荐答案
就我而言,在我尝试了所有这些建议但没有成功之后,我联系了 Microsoft 支持,他们的建议是简单地更改密码.
In my case after I tried all this suggestion without luck, I contacted Microsoft support, and their suggestion was to simply change the password.
这解决了我的问题.
注意密码没有过期,因为我成功登录了office365,但是重置解决了问题.
Note that the password wasn't expired, because I logged on office365 with success, however the reset solved the issue.
经验教训:不要相信 Office 365 密码的过期日期,在我的情况下,密码会在 1-2 个月后过期,但它不起作用.这促使我在我的代码中进行调查,直到很长时间之后我才意识到问题出在损坏"或过早过期"的 Office365 密码中.
Lesson learned: don't trust the Office 365 password expiration date, in my case the password would be expired after 1-2 months, but it wasn't working.This leaded me to investigate in my code and only after a lot of time I realized that the problem was in the Office365 password that was "corrupted" or "prematurely expired".
不要忘记每 3 个月刷新"一次密码.
Don't forget every 3 months to "refresh" the password.
这篇关于尝试通过 Office 365 发送电子邮件时出现 SMTP 5.7.57 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!