It's very slow Help!!

Sep 24, 2014 at 11:23 AM
Edited Sep 24, 2014 at 11:27 AM
Hey guys. I use this component with gmail but it's too slow. My codes are.. My data contains 5 row from gmail but. It's very slow why is that. ( Sorry about my english )

Imap4Client imap = new Imap4Client();
        imap.ConnectSsl("imap.gmail.com", 993);
        imap.LoginFast("support@******", "*******");
        imap.Command("capability");

        Mailbox inbox = imap.SelectMailbox("inbox");
        Fetch Fec = inbox.Fetch;

        List<int> all_ids = inbox.Search("ALL").ToList();
        foreach (int item in all_ids)
        {
            try
            {
                Message msg_first = Fec.MessageObject(item);                   
                SupportMails sm = new SupportMails();
                sm.detail = msg_first.BodyHtml.Text;
                sm.send_date = msg_first.Date;
                sm.topic = msg_first.Subject;
                sm.sender_name = msg_first.From.Name;
                sm.sender_mail = msg_first.From.Email;
                sm.id = item;
                hepsi.Add(sm);
            }
            catch 
            {
                continue;
            }

        }
Sep 30, 2014 at 4:28 PM
Edited Sep 30, 2014 at 4:29 PM
I see several areas which could be causing some performance problems depending on how many messages this is operating on.

First, the following line of code may be inefficient:
List<int> all_ids = inbox.Search("ALL").ToList();
You don't need that to be a list, you can use an enumerator in your foreach loop like this:
foreach (int item in inbox.Search("ALL"))
The odds of this being a major point of slowness in your code is minimal, however.

More than likely the problem is just network slowness, but MailSystem.NET's MIME parser is also pretty slow from my own testing.

I wrote my own parser (MimeKit) which is almost 2 orders of magnitude faster (2 orders of magnitude meaning almost 100x faster... it actually comes out to something like 75x faster).

When MailSystem.NET did not want to use my parser, I wrote my own IMAP library (which is also more efficient than MailSystem.NET's IMAP implementation as well as being far more robust and standards-compliant) called MailKit.

Hope that helps.