Search code examples
c#office-interop

Retrieve a folder for a specific Outlook account


My application is targetted for Office 2010 and up. I am trying to write a function which processes incoming emails automatically. However, i am finding that the event will only be triggered if the MailItem is received in the default email account. If the user has multiple email accounts added in Outlook, I would like all of them to point to the same event handler if possible.

Here is what i have so far:

var inbox = _outlookApplication.Session.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
items = inbox.Items;

if (inbox != null)
            items.ItemAdd += new ItemsEvents_ItemAddEventHandler(OnIncomingEmail);

I have tried looping through the _outlookApplication.Session.Accounts object, but couldn't find a method that would let me retrieve the folders for a particular account.

Any help would be greatly appreciated!


Solution

  • Instead of using Namespace.GetDefaultFolder, loop through the Namespace.Stores collection and call Store.GetDefaultFolder. Be prepared to handle errors as some stores (especially if they are not default) might not have all or at least some default folders.