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

Magnum picture Magnum · Apr 15, 2013 · Viewed 7.1k times · Source

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

Answer

braj k picture braj k · Jun 7, 2017

Thank you for the pointer guys. However I was having some trouble implementing the same initially. Here is how I solved it, just in case if some one faces the same issue.

Outlook.MAPIFolder parentFolder = mailItemToDelete.Parent as Outlook.MAPIFolder;
string FolderLocation = parentFolder.FolderPath;

The Parent object is dynamic and hence was causing issue.