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):" );
}
};