I am using the following code to find all the emails sent from a user, however this only searches the main Inbox folder and doesn't check any sub-folders. I would like to search all the mail items including any sub-folders.
I have tried the WellKnownFolderName.Root
and WellKnownFolderName.Inbox
, and these only search those folders, not the sub-folders.
private static void SearchItems(string email)
{
ItemView iv = new ItemView(10);
FindItemsResults<Item> fiitems = _service.FindItems(WellKnownFolderName.Inbox, "from:[email protected]", iv);
foreach (Item item in fiitems)
{
Console.WriteLine("Subject:\t" + item.Subject);
Console.WriteLine("Received At:\t\t" + item.DateTimeReceived.ToString("dd MMMM yyyy"));
Console.WriteLine();
}
Console.WriteLine("Press Enter to continue");
Console.ReadLine();
}
Bind(ExchangeService, FolderId) Binds to an existing folder, whatever its actual type is, and loads its first class properties. Bind(ExchangeService, WellKnownFolderName) Binds to an existing folder, whatever its actual type is, and loads its first class properties.
Exchange Web Services (EWS) is a cross-platform API that enables applications to access mailbox items such as email messages, meetings, and contacts from Exchange Online, Exchange Online as part of Office 365, or on-premises versions of Exchange starting with Exchange Server 2007.
Answered on http://social.technet.microsoft.com/Forums/en-US/exchangesvrdevelopment/thread/8c71ace4-43d2-4ba2-88f2-16376dad828f.
I have found some information on the AllItems
folder within Exchange over at Glen's blog. I have ported the PowerShell script to C# as shown below.
private static void SearchItems()
{
ExtendedPropertyDefinition allFoldersType =
new ExtendedPropertyDefinition(13825, MapiPropertyType.Integer);
FolderId rootFolderId = new FolderId(WellKnownFolderName.Root);
FolderView folderView = new FolderView(1000);
folderView.Traversal = FolderTraversal.Shallow;
SearchFilter searchFilter1 = new SearchFilter.IsEqualTo(allFoldersType, "2");
SearchFilter searchFilter2 = new SearchFilter.IsEqualTo(FolderSchema.DisplayName, "allitems");
SearchFilter.SearchFilterCollection searchFilterCollection =
new SearchFilter.SearchFilterCollection(LogicalOperator.And);
searchFilterCollection.Add(searchFilter1);
searchFilterCollection.Add(searchFilter2);
FindFoldersResults findFoldersResults =
_service.FindFolders(rootFolderId, searchFilterCollection, folderView);
if (findFoldersResults.Folders.Count > 0)
{
Folder allItemsFolder = findFoldersResults.Folders[0];
Console.WriteLine("Folder:\t" + allItemsFolder.DisplayName);
ItemView iv = new ItemView(1000);
FindItemsResults<Item> findResults =
allItemsFolder.FindItems("System.Message.DateReceived:01/01/2011..01/31/2011", iv);
foreach (Item item in findResults)
{
Console.WriteLine("Subject:\t" + item.Subject);
Console.WriteLine("Received At:\t\t" + item.DateTimeReceived.ToString("dd MMMM yyyy"));
Console.WriteLine("Is New:\t\t" + item.IsNew.ToString());
Console.WriteLine("Has Attachments:\t\t" + item.HasAttachments.ToString());
Console.WriteLine();
}
}
Console.WriteLine("Press Enter to continue");
Console.ReadLine();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With