【问题标题】:Outlook OpenSharedItem lose User propertiesOutlook OpenSharedItem 丢失用户属性
【发布时间】:2014-09-12 05:17:27
【问题描述】:

我正在尝试在用户属性中存储一些数据,然后将邮件写入 .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 !

有人知道怎么做吗?

我没有使用OpenSharedItem,而是尝试使用Process.Start打开邮件,但在这种情况下,用户属性也是空的......

顺便说一句,这段代码是一个测试样本,所以它不能正确地 dispose 对所有 COM 引用。

【问题讨论】:

    标签: c# outlook outlook-addin


    【解决方案1】:

    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 导出到磁盘。

    【讨论】:

    • 感谢您的帮助。与此同时,我还发现这个链接描述了发生的事情:pcreview.co.uk/forums/…不幸的是,您提出的两种解决方案都无法实现,因为 msg 文件必须存储在专用服务器中。
    【解决方案2】:

    好的,我找到了一个似乎可行的解决方案。为此,我需要使用第 3 方:Redemption

    解决方案是使用自定义 MAPI 属性,而不是 UserProperties 集合。在下面的代码中,“this._item”引用了您需要获取/设置属性的 Outlook.MailItem 对象

    为此,您需要一个 Guid,对于您的加载项始终相同

    private const string customPropId = "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}";
    

    设置属性

    public void SetCustomProperty(string propertyName, string propertyValue)
    {
        var sfe = new SafeMailItem() { Item = this._item };
        var propId = sfe.GetIDsFromNames(customPropId, propertyName);
        sfe.set_Fields(propId, propertyValue);
    }
    

    获取属性:

    public string GetCustomProperty(string propertyName)
    {
        var sfe = new SafeMailItem() { Item = this._item };
        var propId = sfe.GetIDsFromNames(customPropId, propertyName);
    
        var value = sfe.get_Fields(propId);
        if (value != null)
            return value.ToString();
    
        return null;
    }
    

    就是这样

    警告:我还没有在真实情况下测试过这段代码,它只适用于与我的问题中发布的测试用例相同的测试用例

    【讨论】:

      猜你喜欢
      • 2016-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-24
      相关资源
      最近更新 更多