Search code examples
outlookvstointeropoutlook-addinoffice-addins

VSTO Outlook: How to get the StoreID from a Outlook.MailItem


I have several accounts configured in Outlook. Now I am trying to get the StoreID of an Outlook mailItem. For example if I select a message from the inbox folder of an account, let's say, account1, I want to get its StoreID, and if I select a message from the inbox folder of another Outlook account, let's say, account2, I want to get the its corresponding StoreID.

I have created an extension method to get the StoreID:

    public static string GetStoreID(this Outlook.MailItem omi)
    {
        Outlook.Folder folder = null;
        Outlook.Store store = null;

        string storeID = null;

        try
        {
            folder = (Outlook.Folder)omi.Parent;
            store = folder.Store;

            storeID = store.StoreID;
        }
        catch (Exception ex)
        {
            Log.Error("GetStoreID: An error occurred while getting mail item storeID. " + ex.ToString());
        }
        finally
        {
            folder = null;
            store = null;
        }

        return storeID;
    }

Is it correct or is there any other way which is better?


Solution

  • Your method is fine. Or you can make it even faster - simply retrieve the PR_STORE_ENTRYID MAPI property (DASL name "http://schemas.microsoft.com/mapi/proptag/0x0FFB0102") using MailItem.PropertyAccessor.GetProperty and convert it to a string using MailItem.PropertyAccessor.BinaryToString.