0

I've problem with specific email configuration when I try to use this configuration to send email I got this error

InnerException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. source

I didn't always get this error some times email is sent and some times get the above error.

But if I try to use this configuration to send email from my local host it's working with no problem but in production (cloud server) not always working.

the code:

SmtpClient smtpClient = new SmtpClient()
{
    Host = settings.SMTPServerIP,
    Port = settings.SMTPPort.Value,
    UseDefaultCredentials = false,
    Credentials = new NetworkCredential(settings.SMTPLoginUserName, settings.SMTPLoginPassword),
    EnableSsl = settings.EnableSSL.HasValue ? settings.EnableSSL.Value : false
};

smtpClient.Timeout = 300000;
mail = new MailMessage { From = new MailAddress(settings.SMTPLoginUserID, settings.SMTPLoginUserID)
, Subject = "test", Body = "test", IsBodyHtml = true };
mail.To.Add(em);
mail.Headers.Add("Message-Id", "<" + MessageId + ">");
smtpClient.SendAsync(mail,null);
smtpClient.SendCompleted += delegate(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
    if (e.Error != null)
    {
        success = false;
    }
    if (e.Error != null)
    {
        insertExceptionLogToCCemails(pClientId, "Email status (error)(async):" + (e.Error == null ? "" : (e.Error.Message)) + " stack:" + (e.Error.StackTrace == null ? "" : e.Error.StackTrace) + " InnerException " + (e.Error.InnerException == null ? "" : e.Error.InnerException.Message) + " source:" + (e.Error.Source == null ? "" : e.Error.Source) + " HResult " + (e.Error.HResult == null ? "" : e.Error.HResult.ToString()) + " TargetSite " + (e.Error.TargetSite == null ? "" : e.Error.TargetSite.ToString()) + " HelpLink " + (e.Error.HelpLink == null ? "" : e.Error.HelpLink.ToString()));
    }
    else
    {
        insertExceptionLogToCCemails(pClientId, "Email status (error)(async):" );
    }
};
AnFi
  • 10,493
  • 3
  • 23
  • 47
  • 3
    This doesn't seem like a code problem but more of a networking/configuration issue. – Mark Cilia Vincenti Jun 20 '19 at 10:04
  • if I use any email configuration to send emails it's working except one email .. do you mean the problem in our side (our production server configuration) or the remote email server ? @MarkCiliaVincenti – Premitek 1 Jun 20 '19 at 10:07
  • 1
    Destination smtp server could be down, orr your server could have saturation, or network package lost. Anyway, looks like not code related. – Cleptus Jun 20 '19 at 10:10
  • Try installing smtp4dev and check whether you can reliably send email, first and foremost. https://github.com/rnwood/smtp4dev – Mark Cilia Vincenti Jun 20 '19 at 10:10
  • There might be lots of reasons for this behavior. We had a similar issue in production caused by a firewall. I recommend, reading this answer from a similar thread: https://stackoverflow.com/a/2582070/3460686 – Tho Mai Jun 20 '19 at 10:12

0 Answers0