Noelle
Noelle

Reputation: 782

Retrieve emails from a non WellKnownFolder in Outlook

I am pulling all emails from an Outlook Exchange Inbox using Microsoft.Exchange.WebServices and the code below works perfect

ExchangeService service = EmailCredentials();
FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, new ItemView(CountRec));

foreach (Item i in findResults.Items)
{
  countOfEmails++;
}

Now there is a request to pull the emails from another user created folder outside the inbox. Anything I've found uses MAPI or EAGetMail but I need to use the exchange webservices. Is this possible?

**EDIT

thanks to @farbiondriven using his code with a few tweaks I have it working now with

ExchangeService service = EmailCredentials();

 // Return only folders that contain items.
 SearchFilter searchFilter = new SearchFilter.IsGreaterThan(FolderSchema.TotalCount, 0);

 FolderView view = new FolderView(10);
 // Unlike FindItem searches, folder searches can be deep traversals.

 view.Traversal = FolderTraversal.Deep;
 // Send the request to search the mailbox and get the results.

FindFoldersResults findFolderResults = service.FindFolders(WellKnownFolderName.MsgFolderRoot, searchFilter, view);

foreach (Folder folder in findFolderResults.Folders)
{
    if (folder.DisplayName == "MyFolder")
    {
        FindItemsResults<Item> findResults = service.FindItems(folder.Id, new ItemView(CountRec));

        foreach (Item i in findResults.Items)
        {
            countOfEmails++;
        }
    }
 }

Upvotes: 0

Views: 92

Answers (1)

farbiondriven
farbiondriven

Reputation: 2468

I can't test it unfortunately, but can you try this ?

ExchangeService service = EmailCredentials();

// Return only folders that contain items.
SearchFilter searchFilter = new SearchFilter.IsGreaterThan(FolderSchema.TotalCount, 0);

// Unlike FindItem searches, folder searches can be deep traversals.
view.Traversal = FolderTraversal.Deep;

// Send the request to search the mailbox and get the results.
FindFoldersResults findFolderResults = service.FindFolders(WellKnownFolderName.Root, searchFilter, view);

foreach (var folder in findFolderResults.Folders)
{
    FindItemsResults<Item> findResults = service.FindItems(folder, new ItemView(CountRec));

    foreach (Item i in findResults.Items)
    {
        countOfEmails++;
    }
}

Upvotes: 3

Related Questions