Tribal Knowledge: EWS C# Extract Alternate Email Address’ Mailbox

Posted by OmegaMan at October 20, 2009

Category: Exchange

Tags:

stockxpertcom_id260320_jpg_7150e45f4e023f0d664c8903434549ef 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

Share

19 Comments

  1. Riaz says

    This really helped ..Many thanks.

    Riaz.

    Reply
  2. steve morris says

    Hi,

    I’m having a problem with the service.findfolders reference/function .. my visual studio complains that it’s not in scope .. I’ve added Microsoft.Exchange.WebServices as a reference to the project and have got a using Microsoft.Exchange.WebServices.Data line in the code .. also the other functions and types that you reference.. i.e. “Mailbox” all resolve correctly… help !

    Reply
  3. Mateus says

    Hello,

    Do you have any idea on how we can send an email using this same logic? I have an service account that accesses a mailbox, so I’m facing an issue when I try to send an email using EWS. I’m able to read the emails, to save in a different folder, but not able to send or forward emails from this mailbox. Do you have any idea on how to do it?

    Thanks,
    Mateus

    Reply
  4. William says

    This was a great help, & just what I needed to know, thanks

    Reply
  5. McKinney says

    Very helpful. Thank you for being so concise.

    Reply
  6. Robert says

    This has been perfect!
    Do you have anything on how to retrieve the message body as well?

    Thanks,

    Reply
    • OmegaMan says

      In my other post (link above) I detail getting the Item class. That class holds the body of the email as a property (Body/UniqueBody). HTH

      Reply
  7. M says

    Amazing dude; Thanks alot for sharing

    Reply
  8. Luis Oliveira says

    Hi, i am trying to access to other inbox folder, but when i do this, says that: The error is: The specified object was not found in the store.”

    what is wrong?

    Reply
  9. Andy Thomas says

    Good to see you here! The reason behind this confusion is how one registers a service with Exchange Web Services. Thanks!

    Reply
  10. codeGorilla says

    Hi,
    Does someone knows how i can loop through “findResults”?

    Many thanks

    Reply
  11. Dharma says

    This was a great help. Thanks for posting this.
    I am able to read mails from shared mailbox. But while sending mails, it is saving sent mails in my mailbox instead of shared mailbox. Can you help me to resolve this issue.

    Reply
  12. lenode says

    This was a great find. I thought I was going to have to dive into the delegate mess. This is much cleaner. Thanks.

    Reply
  13. EWS says

    Many many thanks… this saved the day!!

    Reply
  14. Ben with V says

    Great, thank you. I was frightened I would have to use MAPI again after I started to hate it in 2004.

    I just have one problem with FindFolders – I get an exception asking me to provide the primary SMTP address first. Is this an exchange configuration issue or have I failed?

    Again, thank you!

    Reply
  15. ahmedessawy says

    greeat work

    Reply
  16. Alessandro says

    Dear OmegaMan,

    I have a similar problem to the one set by Ben with V on September 6, 2011.
    I have an authenticated domain user, let say User1, and password Password1, but this user is without mailbox. I also have a secondary mailbox associated to User1 named EmailAddress2, and User1 has full access to this mailbox.
    I tried to follow your example but the FindFoldersResults returns no folder (folder count = 0).
    Do you think the problem is related to the fact that User1 does not have a primary mailbox?

    Thanks a lot
    Alessandro

    Reply
  17. John says

    This article is very helpful. I am also trying to move an item to an alternate folder in the Alternate uses mailbox. Do you have an example of how to do this?

    For example. I need to move the item from the Alternate email Inbox to a folder named “ProcessedItems” in the Alternate email account.

    Thanks for your help.

    Reply
  18. CalvinT says

    Thank you! I was looking for this. You save my day!

    Reply

Leave a comment

(required)
(required) (will not be published)

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  • Recent Comments

    Thanks for this simple yet delightful code....
    Hey guys I'm having the same problem as Netanel KL { “Error HRESUL...
    Soluciono el problema con el primero (First step solved it for me)....
    Thank you! I was looking for this. You save my day!...
    Thank you! This was a big help. My requirement was a little differen...
    Thanks, after a small path adjustment it worked for Visual Studio 2015...
    Good call!...
    Thank you .. this helped...
    How about when you have a long string with no space? I tried it and it...
    Thank you, I ran into this same issues with Visual Studio 2015 after i...
  • Blog Stats

    • 138,578 hits