【问题标题】:Uniquely identify Mailitem唯一标识 Mailitem
【发布时间】:2017-07-28 22:31:23
【问题描述】:

我需要为每个使用过的 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];
    }

当模型已经存在时,我会查找并返回它。如果没有创建,我创建它并在 MailItem 被卸载后删除该条目。

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

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

有没有办法区分MailItem 没有以某种方式保存以便可以在Dictionary&lt;,&gt; 中使用。

【问题讨论】:

  • 您可以创建并设置一个UserProperty 来存储唯一ID。
  • 我不需要保存该项目以保留 UserProperty 吗?
  • 如果邮件项没有被用户存储和保存,则ID保持未使用状态。但这应该不是问题。

标签: outlook vsto


【解决方案1】:

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

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 公用文件夹,或从一个个人文件夹 (.pst) 文件移动到另一个 .pst 文件。除非不移动项目,否则解决方案不应依赖 EntryID 属性是唯一的。基本上,只要邮件保留在其父文件夹中,它就可以正常工作,或者如果 Outlook 项目移动到不同的文件夹(取决于商店提供商),它可能会发生更改。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-05
    • 2015-05-08
    • 2012-06-02
    • 2012-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多