Office 365 SMTP开始触发net_io_connectionclosed

Rob*_*Rob 4 c# asp.net smtp office365

前段时间我已经将我的ASP.NET C#项目配置为通过Office 365发送电子邮件,但上周它开始抛出很多例外.

System.IO.IOException: Unable to read data from the transport connection: net_io_connectionclosed. 
at System.Net.Mail.SmtpReplyReaderFactory.ProcessRead(Byte[] buffer, Int32 offset, Int32 read, Boolean readLine) 
at System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader caller, Boolean oneLine) 
at System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader caller) 
at System.Net.Mail.CheckCommand.Send(SmtpConnection conn, String& response) 
at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, Boolean allowUnicode, SmtpFailedRecipientException& exception) 
at System.Net.Mail.SmtpClient.Send(MailMessage message)
Run Code Online (Sandbox Code Playgroud)

我怎样才能防止这种情况发生?

  MailMessage message = new MailMessage(System.Configuration.ConfigurationManager.AppSettings["smtpfrom"], email, strOnderwerp, strBody);
            message.Priority = MailPriority.Normal;
            message.IsBodyHtml = true;
            SmtpClient client = new SmtpClient(System.Configuration.ConfigurationManager.AppSettings["smtpserver"], Convert.ToInt32((System.Configuration.ConfigurationManager.AppSettings["smtpport"])));

            client.EnableSsl = Boolean.Parse(System.Configuration.ConfigurationManager.AppSettings["smtpssl"]); ;
            client.Credentials = new System.Net.NetworkCredential(System.Configuration.ConfigurationManager.AppSettings["smtpuser"], System.Configuration.ConfigurationManager.AppSettings["smtppass"]);

            client.Send(message);
            client.Dispose();
Run Code Online (Sandbox Code Playgroud)

Dispose上似乎抛出异常.

And*_*y K 13

在我们的例子中,我们已经在使用smtp.office365.com端点,但突然我们开始net_io_connectionclosed在其中一台机器上接收数据,而相同的代码在其他机器上运行得很好。调查显示,这些计算机解析smtp.office365.com为不同的 IP 地址,并且其中一台服务器似乎出现了异常行为。

在尝试撰写支持查询时,微软似乎对我们开了个坏玩笑:

最近,我们开始拒绝一定比例的使用 TLS1.0/1.1 进行 SMTP 身份验证的 smtp.office365.com 连接(将于 2022 年初开始完全禁用)。

就是这样。切换到 TLS 1.2解决了整个问题。

  • 这被称为“减速带”,显然,微软*确实*[返回有用的错误消息](https://techcommunity.microsoft.com/t5/exchange-team-blog/new-opt-in-endpoint-available -for-smtp-auth-clients-still/ba-p/2659652): `421 4.7.66 不支持 TLS 1.0 和 1.1。请升级/更新您的客户端以支持 TLS 1.2。访问 https://aka.ms/smtp_auth_tls。` 只是(旧的).NET SmtpClient 类丢弃了此信息并显示(无用的)通用 `net_io_connectionclose` 消息。 (2认同)

小智 8

System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | 安全协议类型.Tls11 | 安全协议类型.Tls12;

这对我有用


小智 5

同样的问题始于2月8日我公司.这个问题会随之而去.

我认为解决了这个问题的是对SMTP服务器地址的更改.

我们原来的SMTP服务器地址是podxxxxx.outlook.com,大部分时间仍然有效.我在门户网站上检查了当前的O365 SMTP服务器地址,它应该是smtp.office365.com.

我改变了我的配置指向这个新地址,问题似乎消失了.我的日志显示更改后的最后24小时内没有错误.

如果错误再次发生,我会更新.


小智 5

如果大家在 2022 年都遇到此问题,可能是 Microsoft 决定停止支持服务端点“smtp.office365.com”的 TLS1.0、TLS1.1。

要解决此问题,请使用“smtp-legacy.office365.com”或将客户端配置为使用 TLS1.2

System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
Run Code Online (Sandbox Code Playgroud)

阅读Microsoft 技术社区的这篇文章了解更多详细信息