Search code examples
c#.netoutlookvstooutlook-addin

VSTO Outlook Addin - Move Selected emails to another Email Accounts


My goal is create VSTO Addin for Outlook, to help user sorting emails via Buttons in Custom Ribbon.

In custom Ribbon i have a button's. The button's are permanently set to a specific folder's in the main email inbox.

Code:

        private void btnMainInboxFolder_Click(object sender, RibbonControlEventArgs e)
        {
        Outlook.Selection selectedItems = Globals.ThisAddIn.Application.ActiveExplorer().Selection;
        Outlook.Folder inboxFolder = Globals.ThisAddIn.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox) as Outlook.Folder;
        Outlook.Folder trashEmailFolder = inboxFolder.Folders["Trash Emails"] as Outlook.Folder;

        foreach (object selectedItem in selectedItems)
        {
            if (selectedItem is Outlook.MailItem mailItem)
            {
                mailItem.Move(trashEmail);
            }
        }
    }

The Problem starts when i have several email accounts mapped in Outlook. When i Selected email from Mail Account 1 how i target folder in inbox of Mail Account 2 ?

thank you for any advice.


Solution

  • If you have multiple accounts configured in Outlook you may find the NameSpace.Stores property helpful. It returns a Stores collection object that represents all the Store objects in the current profile, for example:

    private void EnumerateFoldersInDefaultStore()
    {
        Outlook.Folder root =
            Application.Session.
            DefaultStore.GetRootFolder() as Outlook.Folder;
        EnumerateFolders(root);
    }
    
    // Uses recursion to enumerate Outlook subfolders.
    private void EnumerateFolders(Outlook.Folder folder)
    {
        Outlook.Folders childFolders =
            folder.Folders;
        if (childFolders.Count > 0)
        {
            foreach (Outlook.Folder childFolder in childFolders)
            {
                // Write the folder path.
                Debug.WriteLine(childFolder.FolderPath);
                // Call EnumerateFolders using childFolder.
                EnumerateFolders(childFolder);
            }
        }
    }       
    

    Also you can use the GetDefaultFolder method of the Store class to get any default folder in a specific store. This method is similar to the GetDefaultFolder method of the NameSpace object. The difference is that this method gets the default folder on the delivery store that is associated with the account, whereas NameSpace.GetDefaultFolder returns the default folder on the default store for the current profile.

    The Problem starts when i have several email accounts mapped in Outlook. When i Selected email from Mail Account 1 how i target folder in inbox of Mail Account 2 ?

    Find the right store first and then get the target folder where you need to move items. You may also find the NameSpace.Accounts property which returns an Accounts collection object that represents all the Account objects in the current profile.