2017-07-28 158 views
0

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

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

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

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

有没有办法distingush MailItem的不保存的方式,因此它可以用于Dictionary<,>

+0

您可以创建并设置[UserProperty](https://www.add-in-express.com/creating-addins-blog/2011/08/19/how-to-add-a-custom-property -to-the-userproperties-collection-of-an-e-mail-item-in-outlook /)来存储唯一ID。 –

+0

我是否还需要保存该项目以支持UserProperty? – lokimidgard

+0

如果邮件项目未被用户存储和保存,该ID仍未被使用。但这不应该是一个问题。 –

回答

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 

要知道,当一个项目被移动到另一个存储,例如,从收件箱到Microsoft Exchange服务器的公用文件夹条目号改变,或者从一个个人文件夹(.pst)文件到另一个.pst文件。解决方案不应该依赖于EntryID属性是唯一的,除非项目不会被移动。基本上,只要邮件停留在其父文件夹中,它就可以正常工作,或者如果Outlook项目移动到其他文件夹(取决于商店提供商),它可能会更改。

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