Custom Header Setting in Outlook MailItem

I am working on an add-in add-in where I need to set a custom header. I am using VS2010 for my development.

I am trying to use the following code, but it does not work.

private void AddUserProperty(Outlook.MailItem mail, string folderEmailId)
{

    Outlook.PropertyAccessor mailPropertyAccessor = null;
    try
    {

        if (string.IsNullOrEmpty(folderEmailId))
             return;

        mailPropertyAccessor = mail.PropertyAccessor;
        mail.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/string/{00020386-0000-0000-C000-000000000046}/X-WorksiteFolderEmailId", folderEmailId);

        mail.Save();

        try
        {
             MessageBox.Show("Existing :" + mail.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/string/{00020386-0000-0000-C000-000000000046}/X-WorksiteFolderEmailId"));
        }
        catch (System.Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
   }
   catch (System.Exception ex)
   {
        Logger.Error(ex);
        MessageBox.Show(ex.Message);
   }
   finally
   {
        if (mailPropertyAccessor != null)  
            Marshal.ReleaseComObject(mailPropertyAccessor);
   }
}

After saving the mail item, I try to get the same item for verification, but it throws an exception saying that the property was not found.

+4
source share
1 answer

I do not see a problem with your code, although getting a link directly to the PropertyAccessor is not required. Try:

    string prop = "http://schemas.microsoft.com/mapi/string/{00020386-0000-0000-C000-000000000046}/X-WorksiteFolderEmailId";
    mail.PropertyAccessor.SetProperty(prop, folderEmailId);

    mail.Save();
+1
source

Source: https://habr.com/ru/post/1525786/


All Articles