EWS foreach all unread messages do not work

I need to skip all unread messages in the Inbox and download the first attachment for each letter, my code works, but only for the first letter, why?

/* load all unread emails */
SearchFilter sf = new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false));
FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, sf, new ItemView(1));

/* loop through emails */
foreach (EmailMessage item in findResults)
{
    item.Load();

    /* download attachment if any */
    if (item.HasAttachments && item.Attachments[0] is FileAttachment) 
    {
        Console.WriteLine(item.Attachments[0].Name);
        FileAttachment fileAttachment = item.Attachments[0] as FileAttachment;

        /* download attachment to folder */
        fileAttachment.Load(downloadDir + fileAttachment.Name);
    }

    /* mark email as read */
    item.IsRead = true;
    item.Update(ConflictResolutionMode.AlwaysOverwrite);
}

Console.WriteLine("Done");

in my inbox he set the first email to read, but sript stops and writes "Done". to the console window. What's wrong?

+4
source share
2 answers

The problem is that you are requesting only one item from Exchange.

FindItemsResults<Item> findResults = service.FindItems(
    WellKnownFolderName.Inbox, 
    sf, 
    new ItemView(1));

The constructor of the ItemView class takes page size as its parameter, which is defined as:

, .

, , , foreach .

, pageSize , , 100 1000.

, :

SearchFilter sf = new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false));
FindItemsResults<Item> findResults;
ItemView view = new ItemView(100);
do {
    findResults = service.FindItems(WellKnownFolderName.Inbox, sf, view);
    foreach (var item in findResults.Items) {
        // TODO: process the unread item as you already did
    }
    view.Offset = findResults.NextPageOffset;
}
while (findResults.MoreAvailable);

Exchange ( 100), , .

+4

public void readMail()       {

        ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010);
        service.Credentials = new WebCredentials("uname", "password", "domain");
        service.Url = new Uri("URL");
        System.Net.ServicePointManager.ServerCertificateValidationCallback = ((sender, certificate, chain, sslPolicyErrors) => true);

        SearchFilter searchFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false));
        FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, searchFilter, new ItemView(int.MaxValue));
            foreach (EmailMessage item in findResults.Items)
            {

                    item.Load();
                    if (item.HasAttachments)
                    {
                        foreach (var i in item.Attachments)
                        {
                            try
                            {
                                FileAttachment fileAttachment = i as FileAttachment;
                                 fileAttachment.Load("C:\\Users\\xxxxx\\Desktop\\comstar\\Download\\test\\" + fileAttachment.Name);
                            }

                            catch(Exception e)
                            {
                                Console.Write(e);
                            }

                        }
                    }
                    //set mail as read 
                    item.IsRead = true;
                    item.Update(ConflictResolutionMode.AutoResolve);

                }

    }
+2

All Articles