Archived Forum Post

Index of archived forum posts

Question:

how to Read bulk imap mail box and mark with delete at one time

Apr 29 '13 at 08:22

Hi,

frequently we are getting below error ,our mail box contains more than 1 lakh mails ChilkatLog: Expunge: Not connected to an IMAP server. Not in the authenticated state --Expunge --ChilkatLog

   i had written code like below
        //current settings are 
      ConnectTimeout = 60, 
      ReadTimeout = 30, 
      AutoDownloadAttachments = false

        using (Imap imap = GetImapClient(serverName, username, password))
        {
            int numofEmails = imap.NumMessages;
            for (int currentIndex = 1; currentIndex <= numofEmails; currentIndex += 10)
            {
                try
                {
                    using (EmailBundle bundle = imap.FetchSequence(currentIndex, 10))
                    {
                        if (bundle == null || bundle.MessageCount <= 0)
                        {
                            continue;
                        }
                        //  Process the bundle...
                        for (int j = 0; j < bundle.MessageCount; j++)
                        {
                            using (Email email = bundle.GetEmail(j))
                            {
                                if (ProcessEmail(email))
                                {
                                    imap.SetMailFlag(email, "Deleted", 1);
                                }
                            }
                        }

                    }
                }
                finally
                {
                    imap.Expunge();
                }
            }
        }

current work around i had done checking imap.connection before expunge if connection is not available reconnect again and call the expunge.

we are calling expunge for each 10 mails ,is this correct or expugne should call after marking all the mails with deleted if that the case if the application corrputs before calling expugne and after marking deleted true . please guide. Note: one more observation reading is faster with marking delete ,is their any other way to do above the work.

Thanks and regards, Harish Chepuri


Answer

If a Chilkat IMAP method call that sends a command to the IMAP server returns with a "Not connected to an IMAP server." in the LastErrorText, it means the connection was lost in a previous method call (or the connection was never made in the first place). The return value of each Chilkat IMAP method call must be checked for success/failure. If it failed, you might wish to call the Imap.IsConnected() method (note that it's a method and not a property) to check to see if the connection to the server still exists.

In your code snippet above, the calls to SetMailFlag and Expunge are NOT checked for success/failure.