2
votes

We would like to be able to obtain unread mails from all the mailboxes contained in our exchange server. From there, we will process them in our application and then delete them. The number of unread mails will always be in small proportion.

Our first idea was to create a master mailbox, give him full access (with EPS) to other mailboxes and obtain the unread mails with EWS. We tried with this :

        //Define the service 
        ExchangeService service = new ExchangeService(SERVICE_VERSION);
        service.Url = new Uri(SERVICE_URL);
        service.Credentials = new WebCredentials("MyAdmin", "MyPassword");

        //define our search
        FolderView viewFolders = new FolderView(int.MaxValue) { Traversal = FolderTraversal.Deep, PropertySet = new PropertySet(BasePropertySet.IdOnly) };
        ItemView viewEmails = new ItemView(int.MaxValue) { PropertySet = new PropertySet(BasePropertySet.IdOnly) };
        SearchFilter unreadFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false));

        //search all items in Inbox and subfolders
        FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Root, unreadFilter, viewEmails);

        //bind the result
        ServiceResponseCollection<GetItemResponse> responseList =
               service.BindToItems(findResults.Select(item => item.Id), new PropertySet(BasePropertySet.FirstClassProperties,EmailMessageSchema.ReceivedBy, EmailMessageSchema.From, EmailMessageSchema.Subject, EmailMessageSchema.Attachments));

However this will only get the unread mails from the MyAdmin mailbox. With further investigation, we found this way to obtain unread mail from another specific mailbox :

        //Define the service 
        ExchangeService service = new ExchangeService(SERVICE_VERSION);
        service.Url = new Uri(SERVICE_URL);
        service.Credentials = new WebCredentials("MyAdmin", "MyPassword");

        //define our search
        FolderView viewFolders = new FolderView(int.MaxValue) { Traversal = FolderTraversal.Deep, PropertySet = new PropertySet(BasePropertySet.IdOnly) };
        ItemView viewEmails = new ItemView(int.MaxValue) { PropertySet = new PropertySet(BasePropertySet.IdOnly) };
        SearchFilter unreadFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false));

        //define the user mailbox
        var userMailbox = new Mailbox("[email protected]");
        var folderId = new FolderId(WellKnownFolderName.Inbox, userMailbox);

        //search all items in Inbox
        var userItems = service.FindItems(folderId, unreadFilter, viewEmails);

The problem with this solution is for 3000 mailboxes, we must then query 3000 times the EWS to obtain the unread mails from every mailbox (Knowing that many won't have unread mails).

Is there a way to obtain, in a single call, all the unread mails from every mailboxes of the exchange server? We are using exchange 2013 with the latest EWS.

Thanks in advance

1
I don't think its possible. I think you have to for-each over the users. Is there a follow button in SO, I would like to keep watching this. Up-voted.Seabizkit
here is how to follow a question in SO. Thanks for the upvote. meta.stackexchange.com/questions/86877/…Mathieu Bourgoin
You can do multi mailbox search using eDiscovery msdn.microsoft.com/en-us/library/office/… however IsRead isn't indexed so it isn't a query you could make so your only option is doing 3000 requests.Glen Scales

1 Answers

1
votes

I managed to find a way that is satisfying my requirement to -almost- obtain every unread mails into a single call.

Here's the trick. I added a transport rule with ECP to put MyAdmin in BCC for every incoming email. From there, I process in a single call, as wanted, every unread email for all the mailboxes. I will also keep track of processed emails from my routine in my database and at the end of the day, I will create another routine that will destroy the emails in all the other mailboxes that I have processed.

Simple but clean and working. Hope it will help people.