Outlook 2010 - C# - Get the account associated to a mail

account, add-in, c#, outlook

Solution

You can get the parent folder (`MailItem.Parent`) of an `Outlook.MailItem` to determine its Folder path (`Folder.FolderPath`).

Outlook.Folder parent = MailItem.Parent as Outlook.Folder;
string itemPath = parent.FolderPath;

Problem

I'm creating an Outlook add-in that can save selected emails to an external database. Using the `Office.IRibbonControl` I can get the list of the selected email, but I need to know to which account those mails are associated. I mean, if Outlook get messages from `toto@exemple.com` and from `otot@exemple.com`, when I want to save a message I need to know that information. I can't use the informations like sender / receiver because it can be an outcome like an income email. Currently, the only I have found is using the current folder path.. ``` public void SayHello(Office.IRibbonControl control) { MessageBox.Show( "Folder: " + (control.Context as Outlook.Explorer).CurrentFolder.FolderPath, "Test", MessageBoxButtons.OK, MessageBoxIcon.Information); } ``` But the method isn't good enough. If I open a message (in a separated window) and then I change the current folder, it fails. Also, `Outlook.Explorer.CurrentAccount` does not work as I expected. So here is my question : How can I access the related account having a `Outlook.MailItem` object ?

Original source