Search code examples
c#loadexchangewebservices

How to load messages faster? EWS C#


I have a question about loading methods for EWS. I have a lot of messages in my outlook and I'm also using EWS and WinForms.

Right know my code is this:

while (more)
        {
            FindItemsResults<Item> findResults1 = allItemsFolder.FindItems("System.Message.DateReceived:01/01/2011..12/31/2022", iv);

            foreach (var item in findResults1)
            {
                if (item.GetType() == typeof(Microsoft.Exchange.WebServices.Data.EmailMessage))
                {
                    listik.Add(item);
                }
            }
            more = findResults1.MoreAvailable;
            if (more)
            {
                iv.Offset += 1000;
            }
        }

It took 12 minutes already for me for running my application and I decided to stop it, because something is wrong. I think I have more than 61k messages.

The idea is to show only those messages, which have my criteria, like email address. So, I should go through all the messages and folders and put suitable in one list to show them. I'm not sure how to do this better.

Anyone can help me with that?

Edit//Added more code

        PropertySet psPropSet = new PropertySet(BasePropertySet.FirstClassProperties)
        {
            RequestedBodyType = BodyType.Text
        };

Added more code:

foreach (var item in listik)
            {
                if (item != null)
                {
                    if (((EmailMessage)(item))?.From?.Address != null)
                    {
                        var d = ((EmailMessage)(item)).From.Address;
                        if (d.Contains(comboBox2.SelectedItem.ToString()))
                        {
                            item.Load(psPropSet);

                            emails.Add((EmailMessage)item);

                            if (item.Subject != null)
                            {
                                listBox1.Items.Add(item.Subject);
                            }
                        }
                    }
                }
            }

I found a link from Microsoft: https://learn.microsoft.com/en-us/exchange/client-developer/exchange-web-services/email-properties-and-elements-in-ews-in-exchange

It's about first-class properties and I can see here EWS element - FROM, but I can't figure out how to set it in iv.PropertySet.


Solution

  • I found the way how it's working. Thanks, everyone for your help.

    iv.PropertySet = new PropertySet(EmailMessageSchema.From, ItemSchema.Subject);
            while (more)
            {
                FindItemsResults<Item> findResults1 = allItemsFolder.FindItems(iv);
    
                foreach (var item in findResults1)
                {
                    if (item.GetType() == typeof(Microsoft.Exchange.WebServices.Data.EmailMessage))
                    {
                        listik.Add(item);
                    }
                }
                more = findResults1.MoreAvailable;
                if (more)
                {
                    iv.Offset += 1000;
                }
            }