Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Email delivery

SmtpException: Unable to read data from the transport connection (net_io_connectionclosed)

ErrorSystem.Net.Mail.SmtpException: Unable to read data from the transport connection: net_io_connectionclosed.

Short answer

The server closed the connection while SmtpClient was waiting for a reply. The common reasons: you pointed SmtpClient at port 465, which expects TLS from the first byte and SmtpClient cannot do that; TLS is off where the server requires it; or the client's TLS version is too old. Use port 587 with EnableSsl, or MailKit.

  • Applies to .NET 8, .NET Framework 4.8, MailKit 4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print the host, port and EnableSsl value the client really uses at the moment of the send, because config files and defaults often differ from what you expect.

    Console.WriteLine($"{client.Host}:{client.Port} ssl={client.EnableSsl}");
  2. 2Match what you see, then jump to its fix

    1. 1The exception appears at once, and the code uses port 465.SmtpClient is pointed at port 465Fix
    2. 2The connection closes on port 587 with EnableSsl false.TLS is off but the server requires itFix
    3. 3Only against smtp.office365.com, mostly from old .NET Framework apps or old servers.The client offers TLS 1.0 or 1.1 to Microsoft 365Fix
    4. 4Settings look right, and the server only speaks modern options.SmtpClient itself is the limitFix

What does the error look like?

  • System.Net.Mail.SmtpException: Unable to read data from the transport connection: net_io_connectionclosed.

Cause 1 of 4

SmtpClient is pointed at port 465

1Check

Port 465 is the SMTPS style, where TLS starts before any SMTP command. SmtpClient only supports STARTTLS, where the session starts unencrypted and then switches, and Microsoft states that the up-front SSL method is not supported.

grep -rn -E "465|Port *=" src/

2Fix

Change to the submission port 587 with EnableSsl = true. If the provider only offers 465, use MailKit with SslOnConnect.

var client = new SmtpClient("smtp.example.com", 587) { EnableSsl = true };

// or, MailKit on 465
await smtp.ConnectAsync("smtp.example.com", 465, SecureSocketOptions.SslOnConnect);

3Verify

Send a test message and the exception is gone.

dotnet run

Confirmed by learn.microsoft.com

Did this fix it?

Cause 2 of 4

TLS is off but the server requires it

1Check

EnableSsl defaults to false. Microsoft 365 client submission needs TLS enabled on port 587 (or 25), so a plain connection is dropped.

grep -rn "EnableSsl" src/

2Fix

Set EnableSsl to true, and for Microsoft 365 use smtp.office365.com on port 587.

client.Host = "smtp.office365.com";
client.Port = 587;
client.EnableSsl = true;

3Verify

The send completes, or the error changes to an authentication error you can act on.

dotnet run

Confirmed by learn.microsoft.com

Did this fix it?

Cause 3 of 4

The client offers TLS 1.0 or 1.1 to Microsoft 365

1Check

Microsoft rejects a share of SMTP AUTH connections that use TLS 1.0 or 1.1, and requires TLS 1.2 or higher.

echo | openssl s_client -starttls smtp -connect smtp.office365.com:587 -tls1_2 -brief

2Fix

Update the app or runtime so it negotiates TLS 1.2 or higher. Only if a legacy client cannot be updated, Microsoft has an opt-in: allow legacy TLS clients with Set-TransportConfig -AllowLegacyTLSClients $true and submit to smtp-legacy.office365.com.

Set-TransportConfig -AllowLegacyTLSClients $true

3Verify

A send from the app succeeds.

dotnet run

Confirmed by learn.microsoft.com

Did this fix it?

Cause 4 of 4

SmtpClient itself is the limit

1Check

Microsoft says SmtpClient does not support many modern protocols and is not recommended for new development.

dotnet list package | grep -i mailkit

2Fix

Move the sending code to MailKit, which lets you pick the secure option per port.

using var smtp = new MailKit.Net.Smtp.SmtpClient();
await smtp.ConnectAsync("smtp.example.com", 587, SecureSocketOptions.StartTls);
await smtp.AuthenticateAsync(user, password);
await smtp.SendAsync(message);

3Verify

The message is sent and the connection closes cleanly.

dotnet run

Confirmed by learn.microsoft.com

Did this fix it?

How do I stop it coming back?

  • Keep host, port and security mode together in one config section so they cannot drift apart.
  • Use the secure option that matches the port: StartTls for 587, SslOnConnect for 465.
  • Write new sending code with MailKit.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Email

Django send_mail fails or hangs with an SSL error

smtplib.SMTPException raised from send_mail()

Django has two secure modes that must not be mixed: EMAIL_USE_TLS for explicit TLS (STARTTLS, usually port 587) and EMAIL_USE_SSL for implicit TLS (usually port 465).

4 causes, each with a fix

Email

Mail from your own domain keeps landing in the spam folder

dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=example.com

Open a message that went to spam and read its Authentication-Results header: it shows whether SPF, DKIM and DMARC passed, and for which domain.

5 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.