I need to save permanently some custom information inside an Outlook Mailitem and retrieve them back. My case scenario is the following: I select an email in Outlook 2013 explorer, write inside that email some information, save it on my disk as .msg file, delete the email from the Inbox Outlook folder, open the .msg file as Outlook.MailItem and read that information in order to keep them or change them.
I'm using the Outlook.ItemProperties.Add() method to add a custom ItemProperty inside the selected MailItem.
When I get a MailItem from the .msg I'm not able to find that properties.
I've used OutlookSpy->Miscellaneous->OpenIMsgOnIstg function to check the .msg and I've noted that all properties are in the GetProps tab.
My question is: How can I read the properties?
Here is the code to write the properties and save the mail as .msg:
Outlook.Application outApp= new Outlook.Application();
Outlook.MailItem mail= null;
try { mail = (Outlook.MailItem)outApp.ActiveInspector().CurrentItem; }
catch {
if (outApp.ActiveExplorer().Selection.Count > 0)
mail = (Outlook.MailItem)outApp .ActiveExplorer().Selection[1];
else { // ERROR }
}
Outlook.ItemProperty prop01 = mail.ItemProperties.Add("MyProperty01", Outlook.OlUserPropertyType.olText);
prop01.Value = "hola";
Outlook.ItemProperty prop02 = mail.ItemProperties.Add("MyProperty02", Outlook.OlUserPropertyType.olNumber);
prop02.Value = 23;
mail.Save();
mail.SaveAs(@"C:\WorkSpace\mail.msg", Outlook.OlSaveAsType.olMSG);
if (mail != null) Marshal.ReleaseComObject(mail);
if (outlookObj != null) Marshal.ReleaseComObject(outApp );
Here is my code to read the properties from the .msg after I've deleted the email in Outlook:
Outlook.Application outApp= new Outlook.Application();
Outlook.MailItem msgMail=(Outlook.MailItem)outApp.CreateItem(Outlook.OlItemType.olMailItem);
msgMail = (Outlook.MailItem)outApp.Session.OpenSharedItem(@"C:\WorkSpace\mail.msg");
Outlook.ItemProperties mailProps = msgMail.ItemProperties;
Outlook.ItemProperty pr = mailProps["MyProperty01"]; // pr IS NULL
But OutlookSpy shows the properties are in the .msg file.
I have solved my problem following this bypassing solution.