I'm attempting to use EWS 2010 Managed API to get the total size of a user's mailbox. I haven't found a web service method to get this data, so I figured 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'm not understanding what it's asking me to do, or that method just doesn't work with EWS 2010.
Noodling around in the code insight, I was able to write what I thought was a method that would traverse the folder structure recursively and result in a combined 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;
}
(Assumes there aren't more than 10,000 folders inside a given folder. Figure that's a safe bet...)
Unfortunately, this doesn't work.
I'm initiating the 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 null object reference.
For clarity, I also attempted to just get the size of the root folder:
Console.Write(root.ManagedFolderInformation.FolderSize.ToString());
Which threw the same NRE exception, so I know that it's not just that once you get to a certain depth in the directory tree that ManagedFolderInformation doesn't exist.
Any ideas on how to get the total size of the user's mailbox? Am I barking up the wrong tree?
static double ConvertBytesToMb(long bytes) { return (bytes / 1024f) / 1024f; }
– Goldia