I am trying to use the EWS 2010 managed API to get the total size of a user's mailbox. I did not find a web service method to get this data, so I decided that I would try to calculate it. I found one seemingly applicable question on another site about finding mailbox sizes with EWS 2007 , but either I don’t understand what it asks me to do, or this method just does not work with EWS 2010.
Hiding in the insight of the code, I was able to write what I thought was a method that would recursively traverse the folder structure and result in a total for all folders inside the Inbox:
private int traverseChildFoldersForSize(Folder f) { int folderSizeSum = 0; if (f.ChildFolderCount > 0) { foreach (Folder c in f.FindFolders(new FolderView(10000))) { folderSizeSum += traverseChildFoldersForSize(c); } } folderSizeSum += (int)f.ManagedFolderInformation.FolderSize; return folderSizeSum; }
(Assume that there are no more than 10,000 folders in this folder. The figure shows that the safe bid ...)
Unfortunately this does not work.
I initiate recursion with this code:
Folder root = Folder.Bind(svc, WellKnownFolderName.Inbox); int totalSize = traverseChildFoldersForSize(root);
But a Null Reference exception is thrown, essentially saying that [folder].ManagedFolderInformation is a reference to a null object.
For clarity, I also tried just getting the size of the root folder:
Console.Write(root.ManagedFolderInformation.FolderSize.ToString());
Which threw the same NRE exception, so I know that this is not the case as soon as you get to a certain depth in the directory tree that ManagedFolderInformation does not exist.
Any ideas on how to get the total size of a user's mailbox? Am I barking the wrong tree?