SmtpException: Unable to read data from the transport connection (net_io_connectionclosed)
System.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.
Fix it fast
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}");2Match what you see, then jump to its fix
- 1
The exception appears at once, and the code uses port 465.SmtpClient is pointed at port 465Fix - 2
The connection closes on port 587 with EnableSsl false.TLS is off but the server requires itFix - 3
Only 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
Settings look right, and the server only speaks modern options.SmtpClient itself is the limitFix
- 1
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 runConfirmed by learn.microsoft.com
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 runConfirmed by learn.microsoft.com
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 -brief2Fix
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 $true3Verify
A send from the app succeeds.
dotnet runConfirmed by learn.microsoft.com
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 mailkit2Fix
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 runConfirmed by learn.microsoft.com
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
Gmail rejects your mail with 530 5.5.1 Authentication Required
530 5.5.1 Authentication Required
Gmail refuses to accept a message from a connection that has not signed in.
3 causes, each with a fixCodeIgniter email does not send and shows no error message
$email->printDebugger() prints an empty result
CodeIgniter signals a failed email only by send() returning false, with no exception.
4 causes, each with a fixDjango 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 fixDKIM shows 'bad version' or 'unknown key type' because the DNS key record is malformed
dkim=fail (unknown key type)
These results mean the receiver found your DKIM key record but could not use it.
4 causes, each with a fixDovecot refuses to start: Can't load SSL certificate, no valid PEM certificate
doveconf: Error: ssl enabled, but ssl_cert not set
Dovecot reads the ssl_cert setting as the file's contents, not a path, so in 2.3 the value must start with a less-than sign.
3 causes, each with a fixMail 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