将自定义属性永久保存在Outlook MailItem中
本文关键字:Outlook MailItem 存在 保存 自定义属性 | 更新日期: 2023-09-27 18:27:03
我正在编写一个简单的C#程序,该程序试图将一些自定义属性作为元数据存储在Outlook MailItem中…我创建了这个简单的方法来编写一个属性:
public static void AddCustomPropertyToEmail(Outlook.MailItem mail, string propKey, object propValue){
if (propValue is System.Int32) { // INT
mail.UserProperties.Add(propKey, Outlook.OlUserPropertyType.olInteger,true, Outlook.OlFormatInteger.olFormatIntegerPlain);
}
else if (propValue is System.Double){ // DOUBLE
mail.UserProperties.Add(propKey,Outlook.OlUserPropertyType.olCurrency,true,Outlook.OlFormatCurrency.olFormatCurrencyDecimal);
}
mail.UserProperties[propKey].Value = propValue;
mail.Save();
}
这是为了读取单个属性:
public static string GetCustomPropertyFromEmail(Outlook.MailItem mail, string propKey){
return (mail.UserProperties[propKey] != null) ? mail.UserProperties[propKey].Value.ToString() : null;
}
当我在控制台打印时,只有当我从存储在内存中的相同MailItem类型读取属性时,我在打印前添加的每个属性才会正常…例如:
static void Main(string[] args){
Outlook.Application outlookObj = new Outlook.Application();
Outlook.MailItem m1 = GetCurrentEmailItem(outlookObj);
AddCustomPropertyToEmail(m1, “Int”, 100);
AddCustomPropertyToEmail(m1, “Double”, 2.0003);
Console.WriteLine(GetCustomPropertyFromEmail(m2, “Int”));
Console.WriteLine(GetCustomPropertyFromEmail(m2, “Double”));
if (m1 != null) Marshal.ReleaseComObject(m1);
Console.ReadKey();
}
这个程序运行良好,可以打印值!!!
但下面的代码没有,它在每种情况下都返回null:
static void Main(string[] args){
Outlook.Application outlookObj = new Outlook.Application();
Outlook.MailItem m1 = GetCurrentEmailItem(outlookObj);
AddCustomPropertyToEmail(m1, “Int”, 100);
AddCustomPropertyToEmail(m1, “Double”, 2.0003);
m1.SaveAs(@”C:'PWS'myMail.msg”, Outlook.OlSaveAsType.olMSG);
Outlook.MailItem m2 = (Outlook.MailItem)outlookObj.CreateItem(Outlook.OlItemType.olMailItem);
m2 = (Outlook.MailItem)outlookObj.Session.OpenSharedItem(@”C:'PWS'myMail.msg”);
// HERE THE PROGRAM PRINTS NULL
Console.WriteLine(GetCustomPropertyFromEmail(m2, “Int”));
Console.WriteLine(GetCustomPropertyFromEmail(m2, “Double”));
if (m2 != null) Marshal.ReleaseComObject(m2);
Console.ReadKey();
}
我的问题是:我如何在MailItem中保存/存储自定义属性,并可能在PWS中保存相关的msg文件,然后第二次读取这些自定义属性???
您需要调用MailItem.Save来保持更改。