唯一标识 Mailitem

Uniquely identify Mailitem

我需要为每个使用过的 MailItem 存储一个模型。为此,我编写了以下方法

    private readonly static Dictionary<string, PermitCustomPaneViewmodel> ViewmodelLookup = new Dictionary<string, PermitCustomPaneViewmodel>();

    public static PermitCustomPaneViewmodel CreateOrGet(MailItem c)
    {
        if (c.EntryID == null)
            c.Save();
        if (!ViewmodelLookup.ContainsKey(c.EntryID))
        {
            var vm = new PermitCustomPaneViewmodel(c);
            c.Unload += () => ViewmodelLookup.Remove(c.EntryID);
            ViewmodelLookup.Add(c.EntryID, vm);
        }
        return ViewmodelLookup[c.EntryID];
    }

当模型已经存在时,我查找它并return它。如果未创建,我将创建它并在卸载 MailItem 后删除该条目。

但是我观察到 MailItem 对象在调用卸载之前不会一直有效。为了可靠地识别 MailItem,我使用了 EntryID。现在的问题是这只有在项目被保存时才有效。

所以目前如果没有找到 EntryID,我会保存该项目。但这会自动将项目保存在草稿下。

有没有办法区分未以某种方式保存的 MailItem,以便可以在 Dictionary<,>.

中使用

新创建的项目没有 EntryID 属性 集。获取商店提供商分配的 ID,您必须保存它。如果您需要识别新的 MailItem object,您可以考虑使用 UserProperties.Add 方法将用户 属性 添加到该项目,该方法会在 UserProperties collection。例如:

Sub AddUserProperty() 
 Dim myItem As Outlook.ContactItem 
 Dim myUserProperty As Outlook.UserProperty 

 Set myItem = Application.CreateItem(olContactItem) 
 Set myUserProperty = myItem.UserProperties _ 
 .Add("LastDateSpokenWith", olDateTime) 
 myItem.Display 
End Sub

请注意,当一个项目被移动到另一家商店时,条目 ID 会发生变化,例如,从您的收件箱到 Microsoft Exchange Server public 文件夹,或从一个个人文件夹 (.pst) 文件到另一个 .pst 文件。解决方案不应依赖于 EntryID 属性 是唯一的,除非项目不会被移动。基本上,只要邮件保留在其 parent 文件夹中,它就可以正常工作,或者如果 Outlook 项目移动到不同的文件夹(取决于商店提供商),它可能会被更改。

您也可以考虑使用消息 MIME header(PR_INTERNET_MESSAGE_IDPR_TRANSPORT_MESSAGE_HEADERS)中的消息 ID。但是它们没有设置在新创建的项目上。这些属性在从 SMTP 服务器或通过 SMTP 连接器接收的邮件中可用。