SmtpException: Unable to read data from the transport connection: net_io_connectionclosed
EDIT: Super Redux Version
Try port 587 instead of 465. Port 465 is technically deprecated.
After a bunch of packet sniffing I figured it out. First, here's the short answer:
The .NET SmtpClient
only supports encryption via STARTTLS. If the EnableSsl
flag is set, the server must respond to EHLO with a STARTTLS, otherwise it will throw an exception. See the MSDN documentation for more details.
Second, a quick SMTP history lesson for those who stumble upon this problem in the future:
Back in the day, when services wanted to also offer encryption they were assigned a different port number, and on that port number they immediately initiated an SSL connection. As time went on they realized it was silly to waste two port numbers for one service and they devised a way for services to allow plaintext and encryption on the same port using STARTTLS. Communication would start using plaintext, then use the STARTTLS command to upgrade to an encrypted connection. STARTTLS became the standard for SMTP encryption. Unfortunately, as it always happens when a new standard is implemented, there is a hodgepodge of compatibility with all the clients and servers out there.
In my case, my user was trying to connect the software to a server that was forcing an immediate SSL connection, which is the legacy method that is not supported by Microsoft in .NET.
How to resolve Unable to read data from the transport connection: net_io_connectionclosed error while sending email using SMTPClient
Adding this code before creating the smtp client worked for me.
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
| SecurityProtocolType.Tls11
| SecurityProtocolType.Tls12;
}
SMTPException : Unable to read data from the transport connection: net_io_connectionclosed
If you are on a residential internet connection, quite often your ISP will block outgoing email sends by blocking all outbound connections to port 25. This is quite common here in the US. Try connecting to a local email server over TCP/IP, or to one on your own internal network.
Unable to read data from the transport connection?
The error you're getting could come from a plethora of things from a failed connection, to the server rejecting your attempts, to the port being blocked.
I am by no means an expert on SMTP and its workings, however, it would appear you are missing setting some of the properties of the SmtpClient.
I also found that using port 465 is a bit antiquated, and when I ran the code using port 587, it executed without an issue. Try changing your code to something similar to this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Mail;
using System.Net;
namespace EmailTest
{
class Program
{
static void Main(string[] args)
{
SendMail();
}
public static void SendMail()
{
MailAddress ma_from = new MailAddress("senderEmail@email", "Name");
MailAddress ma_to = new MailAddress("targetEmail@email", "Name");
string s_password = "accountPassword";
string s_subject = "Test";
string s_body = "This is a Test";
SmtpClient smtp = new SmtpClient
{
Host = "smtp.gmail.com",
//change the port to prt 587. This seems to be the standard for Google smtp transmissions.
Port = 587,
//enable SSL to be true, otherwise it will get kicked back by the Google server.
EnableSsl = true,
//The following properties need set as well
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(ma_from.Address, s_password)
};
using (MailMessage mail = new MailMessage(ma_from, ma_to)
{
Subject = s_subject,
Body = s_body
})
try
{
Console.WriteLine("Sending Mail");
smtp.Send(mail);
Console.WriteLine("Mail Sent");
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine("Exception caught in CreateTestMessage2(): {0}",
ex.ToString());
Console.ReadLine();
}
}
}
}
Tested to work as well. Also of note: if your Gmail account does not allow "Less Secure" apps to access it, you'll get an error and message sent to your inbox stating an unauthorized access attempt was caught.
To change those settings, go here.
Hope this helps, let me know how it works out.
Send-MailMessage : Unable to read data from the transport connection: net_io_connectionclosed
Office 365 SMTP starts firing net_io_connectionclosed
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.
SendGrid Unable to read data from the transport connection: net_io_connectionclosed
Related Topics
How to Validate a Datetime in C#
Console.Writeline Does Not Show Up in Output Window
Deserialize Json String in to Multiple C# Objects
Convert Time With Milliseconds to Datetime in C#
Convert Time-Formatted Column in Excel to C# Datetime
C# String to Hex , Hex to Byte Conversion
How to Refresh or Show Immediately in Datagridview After Inserting
Fetching Value from a Datatable into Datatable With Where Clause
How to Provide Success Messages ASP.NET MVC
Use Linq to Collaps Multiply Collections into One Collection
Decrypt M3U8 Playlist Encrypted With Aes-128 Without Iv
How to Make Blazor Http Get Json Async Request
How to Rename Keys Within a Json File
Easiest Way to Check If an Arbitrary String Is a Valid Filename
Choosing the Default Value of an Enum Type Without Having to Change Values
How to Call Another Controller Action from a Controller in MVC
How to Format a Number in C# With Commas and Decimals
How to Convert String Formula to a Mathematical Formula in C#