Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Office 365 SMTP starts firing net_io_connectionclosed

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.

like image 922
Rob Avatar asked Mar 18 '23 03:03

Rob


2 Answers

In our case, we were already using smtp.office365.com endpoint, yet all of a sudden we started receiving net_io_connectionclosed on one of our machines, while same code was working perfectly on others. Investigation shown that those machines resolved smtp.office365.com to different IP addresses, and it looked like one of the servers was misbehaving.

On attemt to write support inquiry, it appeared that Microsoft is playing bad trick on us:

Recently, we started rejecting a percentage of connections to smtp.office365.com that uses TLS1.0/1.1 for SMTP AUTH (complete disablement will start early 2022).

And that was just it. Switching to TLS 1.2 fixed whole thing.

like image 132
Andriy K Avatar answered Mar 31 '23 14:03

Andriy K


System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;

this worked for me

like image 34
Kiran Avatar answered Mar 31 '23 14:03

Kiran