0

I send emails from ASP.NET (4.0) website:

SmtpClient mySMTPClient = new SmtpClient();
mySMTPClient.UseDefaultCredentials = false;
mySMTPClient.Host = "email.privateemail.com";
mySMTPClient.Port = 465; //I also tried 587 and none of them
mySMTPClient.Credentials = new System.Net.NetworkCredential("contact@xxxxxx.com", "xxxxxxxxxx");
mySMTPClient.EnableSsl = true;
mySMTPClient.DeliveryMethod = SmtpDeliveryMethod.Network;
mySMTPClient.Send(email);

I send a dozen of emails / day and not in batch or in loop. Only about half of them are sent. The other fail with error:

ERROR MESSAGE: Failure sending mail.
INNER EXCEPTION: System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
   at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
   at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
   --- End of inner exception stack trace ---
   at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
   at System.Net.DelegatedStream.Read(Byte[] buffer, Int32 offset, Int32 count)
   at System.Net.BufferedReadStream.Read(Byte[] buffer, Int32 offset, Int32 count)
   at System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader caller, Boolean oneLine)
   at System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader caller)
   at System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint)
   at System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint)
   at System.Net.Mail.SmtpClient.GetConnection()
   at System.Net.Mail.SmtpClient.Send(MailMessage message)

The failing emails are random. I don't have a pattern.

Windows server 2008 SP2 build 6002 Firewall PfSense - engineer says rules are configured properly, otherwise all emails would fail. 3rd part email provider: they say everything is configured properly with them and they cannot make any modification on their side anyways.

I am not a network engineer and I don't have one. My question is, what do I do to find the root cause so I can address it?

By now I tried Process Monitor but it doesn't help me (or I don't know how to look).

I found several articles on this problem, none help me by now as their solutions don't apply to my context (I tried some with no success).

Thank you

  • Possible duplicate of [An existing connection was forcibly closed by the remote host](https://stackoverflow.com/questions/2582036/an-existing-connection-was-forcibly-closed-by-the-remote-host) – SᴇM Mar 18 '19 at 12:14
  • Based on the exception message, it looks like the mail server is having issues, not your application. It sort of looks like it might be crashing when trying to process the email to send. – Martin Costello Mar 18 '19 at 12:14
  • The two common reason for errors 1) The send mail box is full 2) The email account and the from address (or credential) aren't consistent. Windows only allows Network Credential for the current user (unless you are running as an admin). But if you are running as admin the from address still has match the credentials. Also the email user must have an account on the machine where code is running. The credentials are obtained from from the users who have account on the machine. – jdweng Mar 18 '19 at 12:18
  • @ SeM I red that article. The solution doesn't work for me. I already have TLS 1.2 and strong cryptography. The problem was before implementing TLS 1.2 and having TLS 1.2 didn't improve. @ Martin Costello That's what I told Namecheap who runs Private Email, but they say everything is fine on their side and they won't do anything. I'll try to send all emails from Gmail to see how that works. Good idea. @jdweng Credentials should be fine since the error is only random – Alexandru Cojocaru Mar 18 '19 at 12:48
  • Just make sure the issue is random and not some other factor. I would try to send exactly the same email 100 times and see how may times it fails. – jdweng Mar 18 '19 at 13:08
  • @jdweng I'll do this. Testing with gmail failed and google blocked the ip of my website... – Alexandru Cojocaru Mar 18 '19 at 13:42
  • I thought you said it was intermittent. Last posting sound like it never worked. In c# you have to create a new SMTP (dispose old so there is no memory leak) class for each email since Net 4.0. – jdweng Mar 18 '19 at 14:09
  • @jdweng Some emails are sent, some not. Which object should I dispose, the SmtpClient, the MailMessage or both? – Alexandru Cojocaru Mar 18 '19 at 15:10
  • Only the client is necessary. Net library won't allow the port 465 to be reused unless the connection is closed. Net 4.0 had a bug that closing the connection did not exist so you had to dispose the client. I think that bug has been fixed and a close method was added to SMTP client. Right now i do not have access to newer version of Net to verify. – jdweng Mar 18 '19 at 15:38
  • Disposing the SmtpClient doesn't solve... I will try with "using" – Alexandru Cojocaru Mar 18 '19 at 15:47

1 Answers1

0

This is an issue with NameCheap, I'm sick and tired of their email service, they won't fix it, my code runs fine on Gmail and Hotmail but every now and then fails when using NameCheap's SMTP server with " Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host."

paul-2011
  • 675
  • 10
  • 27