Office 365 SMTP开始触发net_io_connectionclosed

Office 365 SMTP starts firing net_io_connectionclosed
2020-11-21
  •  译文(汉语)
  •  原文(英语)

前一阵子,我已经将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)

如何防止这种情况发生?

  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();

异常似乎抛出在Dispose上.

速聊1:
与您的问题无关,但是我会ConfigurationManager从您的电子邮件发送代码中删除这些呼叫.我将编写一种发送电子邮件的通用方法(例如:)SendEmail,然后将这些值作为变量传递.将调用放入ConfigurationManager任何方法调用中SendEmail.好吧……实际上,我将ConfigurationManager调用包装在特定于返回Config值的规范化类中,因此我可以将其编码为变量MyAppConfigClass.SmtpServer
解决过程1

同样的问题也于2月8日在我的公司开始.问题会来来往往.

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

我们原始的SMTP服务器地址为podxxxxx.outlook.com,并且在大多数情况下仍然有效.我在门户中检查了当前的O365 SMTP服务器地址,它应该是smtp.office365.com.

我将配置更改为指向该新地址,问题似乎已经消失.更改后的最近24小时内,我的日志没有显示任何错误.

如果错误再次发生,我将对此进行更新.

速聊1:
将smtp.office365.com替换为pod51014.outlook.com-希望做到最好.

A while ago I've configured my ASP.NET C# project to send e-mail via Office 365, but last week it's starting to throw a lot of exceptions.

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)

How can I prevent this from happening?

  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();

The exceptions seems to be thrown on the Dispose.

Talk1:
Not pertinent to your question, but I would get those calls to ConfigurationManager out of your email sending code. I would write a generic method for sending emails (ex name: SendEmail), and pass those values as variables. Put the calls to ConfigurationManager in whichever method calls SendEmail. Well ... actually I'd wrap my ConfigurationManager calls in a normalized class specific to returning Config values, so I can code it like variable MyAppConfigClass.SmtpServer
Solutions1

This same issue started at my company on February 8th. The problem would come and go with no pattern.

What I believe solved the problem was a change to the SMTP server address.

Our original SMTP server address was podxxxxx.outlook.com and still works most of the time. I checked for the current O365 SMTP server address in our portal and it should be smtp.office365.com.

I changed my config to point to this new address and the problem seems to have gone away. My logs show no errors for the last 24+ hours after the change.

If the error starts happening again I will update this.

Talk1:
Replaced pod51014.outlook.com by smtp.office365.com - hoping for the best.
转载于:https://stackoverflow.com/questions/28362132/office-365-smtp-starts-firing-net-io-connectionclosed

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入