System.Net.Sockets.SocketException (0x80004005): An established connection was aborted by the software in your host machine

Oct 10, 2014 at 1:20 PM

Please help me related to this issue.

InnerException = System.Net.Sockets.SocketException (0x80004005): An established connection was aborted by the software in your host machine
at System.Net.Sockets.NetworkStream.BeginWrite(Byte[] buffer, Int32 offset, Int32 size, AsyncCallback callback, Object state)
Message = Unable to write data to the transport connection: An established connection was aborted by the software in your host machine.
Source : HigLabo.Net
StackTrace : at HigLabo.Net.SocketClient.Send(Stream stream)
at HigLabo.Net.Smtp.SmtpClient.SendMailList(IEnumerable1 commandList)
at HigLabo.Net.Smtp.SmtpClient.SendMail(SendMailCommand command)
at IQEmailExtractor.EmailManager.SendEmail(String strFrom, String strTo, String strSubject, String strBody, List
1 lstMailAttachment, String strFileAttachment)
TargetSite : Void Send(System.IO.Stream)
HelpLink : No HelpLink Found
Oct 13, 2014 at 8:55 AM
Please give me solution i am waiting for your reply.
Coordinator
Oct 13, 2014 at 11:41 PM
Hi.

Can you show me reproduct code that cause your bug?
Oct 23, 2014 at 8:49 AM
Hi Higty... We are not getting this issue frequently but sometimes we are getting this issue. but since you want to see the code I am posting my code.
  public static void SendEmail(string strFrom, string strTo, string strSubject, string strBody, List<MailContent> lstMailAttachment, string strFileAttachment)
        {
            SmtpClient objSmtpClient = new SmtpClient();
            string strTempFileAttachment = Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["TempFolder"]);
            SmtpContent objSmtpContent = new SmtpContent();            
            SmtpMessage objSmtpMessage = new SmtpMessage();
            SendMailResult objSendMailResult;

            try
            {
                // setting the default values to SMTP client
                objSmtpClient.ServerName = Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["SmptServerName"]);
                objSmtpClient.Port = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["SMTPServerPort"]);
                objSmtpClient.UserName = Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["SmtpUserName"]);
                objSmtpClient.Password = Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["SmtpPassword"]);
                objSmtpClient.ReceiveTimeout = 1000000;

                // find attachment 
                if (lstMailAttachment != null)
                {
                    if (lstMailAttachment.Count > 0)
                    {
                        foreach (MailContent objMailContent in lstMailAttachment)
                        {
                            string StrNewFileName = "";
                            string StrOldFileName = "";

                            if ((objMailContent.IsAttachment) && (objMailContent.ContentDisposition.FileName != ""))
                            {
                                SmtpContent objSmtpAttachmentContent = new SmtpContent();
                                StrOldFileName = objMailContent.ContentDisposition.FileName;
                                StrNewFileName = Common.RemoveInvalidFilePathCharacters(StrOldFileName, "");

                                objMailContent.DecodeData(strTempFileAttachment + StrNewFileName); // Saving the Pdf file @ the temp location so we can upload the attachment in the email
                                objSmtpAttachmentContent.LoadFileData(strTempFileAttachment + StrNewFileName); // Uploading the file in the email so we can forward it to the user                                                              
                                objSmtpMessage.Contents.Add(objSmtpAttachmentContent);                                                                                                
                             }
                        }
                    }
                }

                objSmtpMessage.From = new MailAddress(strFrom);

                string[] arrToAddress = strTo.Split(',');

                for (int i = 0; i <= arrToAddress.Length - 1; i++)
                {
                    objSmtpMessage.To.Add(new MailAddress(arrToAddress[i].ToString()));
                }

                objSmtpMessage.BodyText = strBody;
                objSmtpMessage.Subject = strSubject;
                objSmtpMessage.IsHtml = true;

                if (strFileAttachment != "")
                {
                    objSmtpContent.LoadFileData(strFileAttachment);
                }

                objSendMailResult = objSmtpClient.SendMail(objSmtpMessage);

                if (objSendMailResult.SendSuccessful == false)
                {
                    var resultState = objSendMailResult.State; // need to write this in the log file path
                }

                Common.SendEmailFailed = false;
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error Has been Occured While Sending an Email.....");
                Common.SendEmailFailed = true;
                Common.GenerateErrorLogFile(ex, false);
            }
            finally
            {
                //Code is to remove files from temp folder//
                objSmtpClient.Dispose();
                objSmtpContent = null;
                objSmtpMessage = null;
                objSendMailResult = null;

                if (lstMailAttachment != null)
                {
                    foreach (MailContent objMailContent in lstMailAttachment)
                    {
                        string StrNewFileName = "";
                        string StrOldFileName = "";

                        if ((objMailContent.IsAttachment) && (objMailContent.ContentDisposition.FileName != ""))
                        {
                            StrOldFileName = objMailContent.ContentDisposition.FileName;
                            StrNewFileName = Common.RemoveInvalidFilePathCharacters(StrOldFileName, "");

                            File.Delete(strTempFileAttachment + StrNewFileName);
                        }
                    }
                }
            }
        }
    }