Skip to main content
 首页 » 编程设计

c#之发送邮件后 Outlook.MailItem.UserProperty 消失

2024年04月12日33kenshinobiy

我正在尝试在创建 MailItem 时将自定义 UserProperty 添加到它。

我将附件的哈希作为 UserProperty 添加到我的 MailItem 对象。 然后我在 Outlook 中打开我的新 MailItem。

mi = olApp.CreateItem(Outlook.OlItemType.olMailItem) as Outlook.MailItem; 
            Outlook.UserProperties mailUserProperties = null; 
            Outlook.UserProperty mailUserProperty = null; 
 
            mi.Attachments.Add(file.FilePath); 
            mailUserProperties = mi.UserProperties; 
            mailUserProperty = mailUserProperties.Add("AttachementsHash", Outlook.OlUserPropertyType.olText); 
            mailUserProperty.Value = file.Hash; 
            mi.Save(); 
            mi.Display(); 

如果我在发送前使用 OutlookSpy 检查 MailItem.UserProperties,我会发现我的邮件有一个 UserProperty。

然后我在 Outlook 中单击“发送邮件”,然后在 SentItems 文件夹中查看我的邮件。 我可以看到 UserProperties.Count == 0。

如果有人知道为什么我的 UserProperty 消失了,请帮助我并告诉我:)

请您参考如下方法:

经过大量努力,我解决了我的问题。

UserProperties 在发送邮件后被删除。但是我使用了 MailItem.PropertyAccessor.SetProperty 而不是 UserProperties

MSDN Documentation of Property Accessor

        string prop = "http://schemas.microsoft.com/mapi/string/{00020386-0000-0000-C000-000000000046}/PropertyName"; 
        mi.PropertyAccessor.SetProperty(prop, propertyValue.ToString()); 

然后在“ItemAdd”事件上我检查了项目是否已添加到 sentItems。 如果它被添加到 sentItems 我使用这样的结构读取属性:

Outlook.MailItem AddedMail = item as Outlook.MailItem; 
                    string attachmentProperty = AddedMail.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x007D001E"); 

最后我解析字符串以获取我的数据。

希望对大家有帮助:)