I get the error "The server has closed the connection"

0 votes
asked Mar 2, 2015 by rockyshah2015 (750 points)
edited Mar 2, 2015 by Lukas Pokorny

Below is the code for which I get this error but not everytime I get this error quite often..
This line "MailMessage mm = imap.GetMailMessage(val.UniqueId);" of code gives the error.

        string strTemp = "D:\\RahulTemp\\";
        Rebex.Licensing.Key = "==Asw8ngRi512lyhFyNlbzu11kO9zWdXn+xvPOqHEeNwTA==";
        SslMode _security = SslMode.Implicit;
        string[] EmailAccount = new string[] { "faxtest6@iqbackoffice.com" };
        for (int j = 0; j < EmailAccount.Length;j++)
        {
            Imap imap = new Imap();
            imap.LogWriter = new Rebex.FileLogWriter(@"D:\Rahultemp\log.txt", Rebex.LogLevel.Info);
            imap.Connect("mail.iqbackoffice.com", 993, _security);
            imap.Login(EmailAccount[j], "Temp123");
            imap.SelectFolder("Inbox");
            //var lst = imap.GetMessageList();
            imap.Timeout = int.MaxValue;
            ImapMessageCollection lst = imap.Search(ImapSearchParameter.Not(ImapSearchParameter.Deleted));
            foreach (var val in lst)
            {
                MailMessage mm = imap.GetMailMessage(val.UniqueId);
                MailMessage mm2 = new MailMessage();
                AttachmentCollection attcol = mm.Attachments;
                for (int i = 0; i < attcol.Count; i++)
                {
                    //if (attcol[i].FileName.Trim().EndsWith(".pdf") || attcol[i].FileName.Trim().EndsWith(".htm") || attcol[i].FileName.Trim().EndsWith(".html") || attcol[i].FileName.Trim().EndsWith(".doc") || attcol[i].FileName.Trim().EndsWith(".xls") || attcol[i].FileName.Trim().EndsWith(".xlsx") || attcol[i].FileName.Trim().EndsWith(".bmp"))
                    //{
                    MimeEntity me = attcol[i].ToMimeEntity();
                    mm.Save(strTemp + attcol[i].FileName);
                    string str = mm.BodyText;
                    mm2.Attachments.Add(new Attachment(strTemp + attcol[i].FileName));
                    //}
                }
                imap.CopyMessage(val.UniqueId, "Archive");
                imap.DeleteMessage(val.UniqueId);
                Smtp objsmtp = new Smtp();
                objsmtp.Connect("192.168.110.4", 25);
                mm2.From = "noreply@iqbackoffice.com";
                mm2.To = "rahulacc13@gmail.com";
                mm2.BodyText = "hello";
                objsmtp.Send(mm2);
            }
            imap = null;
        }
    }
Applies to: Rebex Secure Mail

1 Answer

0 votes
answered Mar 2, 2015 by Lukas Pokorny (103,050 points)

There doesn't seem to be anything wrong with your code. However, a communication log showing the error and at least several lines of client-server communication before it might really help us to understand what is going on - without it, we can only guess why the server closed the connection.

In any case, something like this should not normally occur. The most likely explanation is that a fatal server-side error caused the session to be closed. For example, the server might have been unable to read the actual content of the message for some reason (such as a database failure), and if it already announced to the client that a certain number of bytes of message data is going to follow, it has no choice but to close the session at this point. A server log should contain more information about this.

From the client's side, there is usually not much you can do. If you are able to retrieve other messages and only a single one is causing the error, you might want to try downloading it later. Alternatively, try copying it into another folder or accessing it through the server's web interface (if it has one) and possibly delete it afterwards to work around the issue.

...