0 votes
by (280 points)
edited

Hi Lukas,

Some days back, you gave us below code for resolving issue while accessing a large mailbox account. In this code, first we are fetching all messages in "ImapMessageCollection ids" using imap.Search() method. And then again we are setting as per chunk size ImapMessageSet. After this again we are doing imap.GetMessageList() and fetching the messages. Aren't we doing repetitive actions? Please explain.

    private ImapMessageCollection ImapSearch(Imap imap, ImapSearchParameter imapSearchParameter)
    {
        try
        {
            // search for messages, receive only Unique ids
            ImapMessageCollection ids = imap.Search(ImapListFields.UniqueId, imapSearchParameter);

            // get only necessary information in small chunks
            ImapMessageCollection messages = new ImapMessageCollection();

            for (int i = 0; i < ids.Count; )
            {
                if (i > 0)
                    System.Threading.Thread.Sleep(DELAY_TIME);

                ImapMessageSet set = new ImapMessageSet();
                for (int j = 0; j < CHUNK_SIZE && i < ids.Count; j++, i++)
                {
                    set.Add(ids[i].UniqueId);
                }

                ImapMessageCollection list = imap.GetMessageList(set, ImapListFields.Envelope | ImapListFields.Body);
                foreach (ImapMessageInfo info in list)
                {
                    messages.Add(info);
                }
            }

            return messages;
        }
        catch (Exception ex)
        {
            logger.Error(string.Format("Error Occured at ImapSearch. Error :{0} ", ex));

            throw ex;
        }
    }
Applies to: Rebex Secure Mail
by (280 points)
edited

Any updates?

1 Answer

0 votes
by (73.5k points)
edited
 
Best answer

Please note the server response to a SEARCH command is only collection of UniqueIds (or SequenceNumbers), but the imap.Search() method do more than just the SEARCH command. It depends which ImapListFields you want to receive.

If you want to receive only ImapListFields.UniqueId or only ImapListFields.SequenceNumber the imap.Search() method only parses the response of the SEARCH command.

If you want to receive more than ImapListFields.UniqueId the imap.Search() method requests two commands. The SEARCH command at first and then appropriate FETCH command according to the SEARCH response and a value of the ImapListFields parameter.

Please compare the log file of the imap.Search(ImapListFields.UniqueId, ImapSearchParameter.All) and the imap.Search(ImapListFields.Envelope | ImapListFields.Body, ImapSearchParameter.All) calls.

Therefore the code above doesn't do repetitive actions. It decreases server load.

by (280 points)
edited

Thanks Lukas for your continued support.

...