VSTO Outlook Addin – 将选定的电子邮件移动到另一个电子邮件帐户

huangapple go评论82阅读模式
英文:

VSTO Outlook Addin - Move Selected emails to another Email Accounts

问题

我的目标是为Outlook创建VSTO插件,以帮助用户通过自定义功能区中的按钮对邮件进行分类。

在自定义功能区中,我有一些按钮。这些按钮会永久地设置在主邮箱收件箱中的特定文件夹。

代码:

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);
        }
    }
}

问题出现在当我在Outlook中映射了多个电子邮件帐户时。当我从“邮件帐户1”中选择邮件时,我如何定位“邮件帐户2”的收件箱中的文件夹?

谢谢您的任何建议。

英文:

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.

答案1

得分: 1

如果您在Outlook中配置了多个帐户,您可能会发现NameSpace.Stores属性非常有用。它返回一个Stores集合对象,代表了当前配置文件中的所有Store对象,例如:

private void EnumerateFoldersInDefaultStore()
{
    Outlook.Folder root =
        Application.Session.
        DefaultStore.GetRootFolder() as Outlook.Folder;
    EnumerateFolders(root);
}

// 使用递归来枚举Outlook子文件夹。
private void EnumerateFolders(Outlook.Folder folder)
{
    Outlook.Folders childFolders =
        folder.Folders;
    if (childFolders.Count > 0)
    {
        foreach (Outlook.Folder childFolder in childFolders)
        {
            // 写入文件夹路径。
            Debug.WriteLine(childFolder.FolderPath);
            // 使用childFolder调用EnumerateFolders。
            EnumerateFolders(childFolder);
        }
    }
}       

此外,您还可以使用Store类的GetDefaultFolder方法来获取特定存储中的任何默认文件夹。此方法类似于NameSpace对象的GetDefaultFolder方法。区别在于,此方法获取与帐户关联的交付存储上的默认文件夹,而NameSpace.GetDefaultFolder返回当前配置文件的默认存储上的默认文件夹。

> 当我在Outlook中映射了多个电子邮件帐户时,问题开始了。当我从邮件帐户1中选择电子邮件时,如何定位邮件帐户2的收件箱中的文件夹?

首先找到正确的存储,然后获取您需要移动项目的目标文件夹。您还可以使用NameSpace.Accounts属性,该属性返回一个代表当前配置文件中所有Account对象的Accounts集合对象。

英文:

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.

答案2

得分: 0

Instead of using Namespace.GetDefaultFolder, use Store.GetDefaultFolder (if you are navigating to the folder relative to a default folder) or get to it by its name/path using Store.Folders collection to drill down to the desired folder.

The target Store object can be retrieved (by name/index/etc.) from Application.Session.Stores collection.

英文:

Instead of using Namespace.GetDefaultFolder, use Store.GetDefaultFolder (if you are navigating to the folder relative to a default folder) or get to it by its name/path using Store.Folders collection to drill down to the desired folder.

The target Store object can be retrieved (by name/index/etc.) from Application.Session.Stores collection.

huangapple
  • 本文由 发表于 2023年6月26日 23:44:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76558249.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定