How to fetch only X (a specific number of) emails?

Topics: Feature Requests, Help requests, Issues
Mar 23, 2015 at 4:51 PM
Fetching all e-mails in a folder (inbox, etc) TAKES TOO LONG, event for a few messages (300), so for a 5000 e-mail account the program will either freeze, crash or any other "not-good" situations.
So, my question is:
How to only fetch 10/20 e-mails from a specific folder?
                        List<ImapX.Message> messages = Program.ImapClient.Folders.Inbox.Search().OrderByDescending(x=>x.UId).ToList();

                        for (int i = 0; i < 10; i++ )
                        {
                            MessageBox.Show(messages[i].Subject);
                            String data = messages[i].Date.ToString();
                            flowLayoutPanel1.Controls.Add(new mail1(messages[i].From.ToString(), messages[i].Subject.ToString(), messages[i].Body.Text, data, stdSz));
                        }
This is the code i'm using now, but it still gets all messages from the inbox, and only adds the first 10 to my message list. Any tips?
Mar 25, 2015 at 10:07 PM
Edited Mar 25, 2015 at 10:09 PM
I have figured it out how to do it, with the help of the god-blessed Internet, and with some luck and SPECIAL THANKS to maylortaylor.
This is the GetMails() i have coded for my application. Fell free to use the code as it's (at this time) the fastest one I could think of. Any suggestions are appreciated.

region GETMAIL2

    Int q=Convert.ToInt32("number of e-mails to be fetched");
    private void GetMails2(string query,int q)
    {
        Size stdSz = new Size(flowLayoutPanel1.Width - flowLayoutPanel1.Padding.Right - flowLayoutPanel1.Padding.Left - 30, mailObjectH);
        flowLayoutPanel1.Controls.Clear();
        try
        {
            Program.ImapClient.Behavior.MessageFetchMode = MessageFetchMode.None;
            var messages = Program.ImapClient.Folders.Inbox.Search("TEXT \"" + query + "\"", ImapX.Enums.MessageFetchMode.GMailExtendedData, q).OrderByDescending(x => x.Date);
            foreach (var msg in messages)
            {
                msg.Download(MessageFetchMode.Minimal);
                String data = msg.Date.ToString();
                flowLayoutPanel1.Controls.Add(new pnlmail1(msg.From.ToString(), msg.Subject.ToString(), msg.Body.Text, data, stdSz));
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Exception fuck: " + ex.Message);
        }
    }
    #endregion