我正在编写一个简单的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;
}
当我在Console上打印时,只有当我从存储在内存中的相同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文件并再次读取这些自定义属性???
答案 0 :(得分:1)
您需要调用MailItem.Save来保留更改。