Naresh
Naresh

Reputation: 2673

Setting Custom Header in Outlook MailItem

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

I am trying with the following code but it doesn't seems to be working.

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 am trying to fetch the same item for verification, but it's throwing an exception saying the property not found.

Upvotes: 4

Views: 3559

Answers (1)

Oliver Gray
Oliver Gray

Reputation: 874

I don't see a problem with your code, although getting a reference directly to the PropertyAccessor is unnecessary. Try:

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

    mail.Save();

Upvotes: 1

Related Questions