Search code examples
c#outlookvstooutlook-2010

How to retrieve the Outlook folder of a mail item (Outlook.MailItem)?


I am getting my default inbox folder via inboxFolder = Globals.ThisAddIn.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox

Elsewhere in my code, I begin doing a foreach loop to extract specific information I want from these MailItems

foreach (var item in this.inboxFolder.Items)
{
   Outlook.MailItem mailItem = (Outlook.MailItem)item;
   //.... doing stuff here
   string SenderEmail = mailItem.SenderEmailAddress;
   string SenderName = mailItem.SenderName;
   string FolderLocation = mailItem.???;  //how to retrieve folder location?
   //.... more stuff here
}

For example: A user may have created a subfolder called 'Test' shown below.

An image of a user-created subfolder in Outlook-2010


Solution

  • Do you mean folder path? Use MAPIFolder.FullFolderPath. Or MAPIFoldert.Name if you only need the name.

    Also keep in mind that the value will be the same for all items in the folder, so there is no reason to evaluate it on each step of the loop.