• Register
0 votes
12 views

Problem :

I am using a SmtpClient library to send the emails using a below code :

SmtpClient smtpclient = new SmtpClient();
smtpclient.Host = "hostname";
smtpclient.Port = 465;
smtpclient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpclient.UseDefaultCredentials = false;
smtpclient.EnableSsl = true;
smtpclient.Credentials = new NetworkCredential("User", "Pass);
smtpclient.Send("from@hostname", "to@hostname", "Subject", "Body");

Above code works fine in the test environment, but when I use it on production SMTP servers, the code fails with a SmtpException "Failure sending mail." with the inner IOException "Unable to read data from the transport connection: net_io_connectionclosed".

I have also confirmed that the firewalls are not the issue. A port opens just fine between a client and a server. I am not sure at all what else could throw above error.

by (3.9k points)  

1 Answer

0 votes

Solution :

I have faced the same problem and was able to solve my problem by changing Smtp Client to the Mailkit. A System.Net.Mail Smtp Client is now not at all recommended to use by the Microsoft because of the security issues and so you should instead be using the MailKit. Using the Mailkit gave me a clearer error messages that I could understand finding a root cause of my problem and it was license issue. You can also get Mailkit by downloading it as the Nuget Package.

Please read documentation about the Smtp Client for more information on following link: https://docs.microsoft.com/es-es/dotnet/api/system.net.mail.smtpclient?redirectedfrom=MSDN&view=netframework-4.7.2

by (36.1k points)  
...