简体   繁体   中英

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?

Your code works, so it appears you are asking for any other approach you could take to get the StoreID string.

Whether one approach is "better" than any other approach is purely subjective and entirely context specific.

Another way you could do this, would be to instead get the StoreID from the Explorer object (rather than the MailItem). If you have an "Explorer Wrapper" class in your add-in, you could have something like the following for your backing-fields and constructor:

private Outlook.Explorer _myExplorer;
private Outlook.Folder _myFolder;
private Outlook.Store _myStore;
private string _myStoreID;  

public OutlookExplorerWrapper(Outlook.Explorer explorer)
            {
                
                _myExplorer = explorer;
                _myFolder = (Outlook.Folder)_myExplorer.CurrentFolder;
                _myStore = _myFolder.Store;
                _myStoreID = _myStore.StoreID;
    
            }

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM