Outlook:如何安全地遍历所有商店并将 ItemAdd 事件设置为已发送邮件的所有默认文件夹?

Outlook: How to safely loop through all stores and and set ItemAdd event to all default folders for sent mail?

如果您的 Outlook 中有更多 accounts/stores 并且希望触发 ItemAdd 事件,例如对于所有已发送的项目文件夹。

这是我目前所拥有的,但事件并未针对所有已发送项目文件夹触发:

foreach (Outlook.Store store in _outlookNameSpace.Stores)
{
    // _SentItems = null;
    // _items = null;

    try
    {
        _SentItems = store.GetDefaultFolder(OlDefaultFolders.olFolderSentMail);

        _items = _SentItems.Items;
        _items.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(items_ItemAdd); // BUGBUG: The problem is probably here, as the object needs to be alive which is firing the event?
    }
    catch
    {
        AppUtils.DoLog("Skipping this store.");
    }

}

这些家伙被定义为全局class变量:

Outlook.NameSpace _outlookNameSpace;
Outlook.MAPIFolder _SentItems;
Outlook.Items _items;

创建一个包装器 class,它将 Items 对象作为其构造函数中的参数,将其保存在一个字段中,并设置一个 ItemAdd 事件处理程序。然后,您可以为每个商店初始化一个包装器,然后存储在一个列表中,以确保包装器(及其 Items 对象)保持活动状态并可以引发事件。