8

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.

Rob
  • 3,556
  • 2
  • 34
  • 53
  • 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` – Walter Stabosz Sep 19 '18 at 18:07

5 Answers5

13

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.

Andriy K
  • 3,302
  • 31
  • 42
  • 2
    It's called a "speedbump", and, apparently, Microsoft *does* [return a useful error message](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 and 1.1 are not supported. Please upgrade/update your client to support TLS 1.2. Visit https://aka.ms/smtp_auth_tls.` It's just that the (old) .NET SmtpClient class throws away this information and shows the (useless) generic `net_io_connectionclosed` message instead. – Heinzi Feb 02 '22 at 10:37
  • 1
    Yes I had to update the SMTP address to `smtp-legacy.office365.com` and enable legacy TLS clients in the Exchange Admin Center -> Settings -> Mail flow 'Turn on use of legacy TLS clients.' [Opt in to the Exchange Online endpoint for legacy TLS clients using SMTP AUTH](https://learn.microsoft.com/en-gb/exchange/clients-and-mobile-in-exchange-online/opt-in-exchange-online-endpoint-for-legacy-tls-using-smtp-auth) – j3ffb Feb 04 '22 at 10:53
7

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

this worked for me

Kiran
  • 71
  • 1
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 09 '22 at 12:03
5

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.

csbelli
  • 116
  • 3
5

If you all face this issue in 2022, that might be the case of Microsoft decided to stop supporting TLS1.0, TLS1.1 for service endpoint "smtp.office365.com".

To resolve it, Either use "smtp-legacy.office365.com" or Configure client to use TLS1.2 by

System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

Read this article from Microsoft Tech Community for more details

0

The holy grail of all this bothering with the net_io_connectionclosed Errors while connecting to Office365 in VB.NET or c# is the TSL Version.

When you are working with a VB.NET oder c# Application wanting to connect, send an receive Msg. to/from O365 you have to force TLS12 in you code.

Just put the last line below your EnableSSL = True an you will be fine:

Last Line to force TLS12

Robert
  • 1