Search code examples
c#outlookoffice-interop

Find multiple mail boxes at root parent?


I got a little pet project for work. We have three mail boxes where two of them we need to get told, when we get new emails. I wanted to make a program for that (mostly just for the learning experience) and dived into the Interop libraries of Microsoft.

The two mailboxes where we need notifications, I first need to locate those mailboxes and then I can assign things like event listeners, etc. But I don't know how to achieve this.

To find the root, I do this:

outlookNameSpace = outlookApplication.GetNamespace("MAPI");

And then I can start dragging out folders and assign them to variables like so:

supportInbox = outlookNameSpace.Folders["Omitted"].Folders["Inbox"];
pensionInbox = outlookNameSpace.Folders["Omitted"].Folders["Inbox"];

But the code says that it cannot find the folders with the names (that I replaced with "Omitted" just for sake of privacy).

What am I doing wrong?


Solution

  • If you are accessing multiple Mailboxes, you have to first ensure that they are both loaded in the current Outlook profile. Otherwise, you need to ensure that the currently logged on user has delegate access to the Mailbox you want to access and use the NameSpace.GetSharedDefaultFolder method to open their Inbox. However, you only have access to the Inbox with Delegate scenarios.

    Otherwise, to get specific folders for a specific Mailbox in the current profile, you can try this approach:

    How to: Obtain a Folder Object from a Folder Path http://msdn.microsoft.com/en-us/library/ff868990(v=office.15).aspx

    You may also need to loop through NameSpace.Stores to get a specific Store object for the Mailbox, and access a folder via Store.GetRootFolder or Store.GetDefaultFolder.