Skip to main content
 首页 » 编程设计

c#之Outlook OpenSharedItem 丢失用户属性

2024年02月20日12over140

我试图在用户属性中存储一些数据,然后将邮件写入 .msg 文件,然后(稍后)重新加载 .msg 文件以读取用户属性。

问题是:重新加载文件后,我不再有任何用户属性。

我使用的是 Outlook 2010 32 位

这是一段代码,展示了这种行为:

Outlook.MailItem originalItem = ((MailItemWrapper)this.Item)._item; 
 
var path = System.IO.Path.GetTempFileName() + ".msg"; 
var propName = "ActionId123456789"; 
 
// Set a user property "ActionId" with value "test" 
var ps = originalItem.UserProperties; 
var p = ps.Find(propName); 
if (p == null) 
    p = ps.Add(propName, Outlook.OlUserPropertyType.olText, Type.Missing); 
p.Value = "test"; 
 
// Save to a temp file 
originalItem.Save(); // --> I also tried without this line 
originalItem.SaveAs(path); 
 
// Chech the the property is correctly set 
p = originalItem.UserProperties[propName]; 
if (p != null) 
    Console.WriteLine(p.Value); // ---> Show 'test' 
 
// Open the temp file 
Outlook.MailItem newItem = AddinModule.CurrentInstance.OutlookApp.Session.OpenSharedItem(path) as Outlook.MailItem; 
 
// Check that the property still exists 
p = newItem.UserProperties[propName]; 
if (p != null) 
    Console.WriteLine(p.Value); // ---> Not executed: p is NULL ! 

有人知道怎么做吗?

我还尝试使用 Process.Start 打开邮件,而不是使用 OpenSharedItem,但在这种情况下,用户属性也为 null ...

顺便说一句,这段代码是一个测试示例,因此它不会处理所有 COM 引用。

请您参考如下方法:

This forum post准确描述了您的问题 - 用户属性未保留在 MSG 中。我也经历过同样的行为,即 changed by Microsoft back in 2007 .

作为解决方法,我只是使用了 hidden Outlook folder将我的 MailItem 与用户属性一起存储,而不是将其导出到磁盘并将其带回。

如果您无法使用此解决方法,您可能需要 work with EWS to store it in a shared mailbox并以这种方式访问​​用户属性,而不是将 MSG 导出到磁盘。