Tribal Knowledge: EWS C# Extract Alternate Email Address’ Mailbox
This is what I call a Tribal Knowledge (TK) type of information. TK is something that is obvious to those who work with it, or have been using it for a long time yet to the new person coming along its a mystery. Accessing a different mailbox using Exchange Web Services (EWS) was such an event for me. I had actually tapped Microsoft Premier Support to get this answer but was able to divine it at the last moment.
Why Is This Confusing?
The reason behind this confusion is how one registers a service with Exchange Web Services. Here is a snippet below:
ExchangeService service = new ExchangeService( ExchangeVersion.Exchange2007_SP1 ); service.AutodiscoverUrl( "Omega.Man@MyCompany.com" );
When we pass the email address to Autodiscover we believe that it is registering the account we want to access. Usually when one is developing against EWS they use their mailbox as the target first and will have no problems. (See my article entitled C#: Getting All Emails From Exchange using Exchange Web Services for how process emails from the inbox.)
Once the developer access the inbox and retrieves emails, the next step is to attempt access to a shared mailbox. That inbox is shown in Outlook and the user’s account has access rights to it. So the developer does this and erroneously uses Autodiscover again:
service.AutodiscoverUrl( "SharedMailboxNotPrimaryAccount@MyCompany.com" );
Well it actually works, sort of… the service doesn’t throw an exception, but when on tries to get email, its the primary email inbox that is gotten and not the specified email! The problem is that autodiscover could frankly care less about the who, its only concerned with the @xxxx.com to facilitate its processing. Very disingenuous eh…
Solution
Get the service with the email address as shown above (either one will work actually), but before EWS mail box processing add these lines:
Mailbox mb = new Mailbox( "SharedMailboxNotPrimaryAddress@MyCompany.com" ); FolderId fid1 = new FolderId( WellKnownFolderName.Inbox, mb ); FindFoldersResults findResults = service.FindFolders( fid1, // was WellKnownFolderName.Inbox, new FolderView( int.MaxValue ) );
That lets EWS know about which mailbox we want to use. It handles the getting of the folder ID (different from the ID you might use in Outlook Interop programming if you got it that way; btw) and all is well.
HTH