How to add headers to an outlook interop mail message

571 Views Asked by At

I am not able to connect my program with an exchange server via smtp. So, I tested with the outlook interop and seems working, at least for sending emails and adding attachments. My problem now is adding the header "disposition-notification-to" to the mail message.

Microsoft.Office.Interop.Outlook.Application application = new Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook.MailItem mailMessage = application.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem) as Microsoft.Office.Interop.Outlook.MailItem;

mailMessage.Subject = subject;
mailMessage.Recipients.Add(emailAddress);
mailMessage.HTMLBody = bodyString;
mailMessage.PropertyAccessor.SetProperty("urn:schemas:mailheader:disposition-notification-to", notificationAddress);

When it come to set the property I get Exception thrown: System.Runtime.InteropServices.COMException in program.exe

I haven't found an example, and most of the time with other libraries like mailkit usually the mail item have an Headers field.

Thanks in advice for any help

2

There are 2 best solutions below

0
Dmitry Streblechenko On

To add a custom MIME header on an outgoing message, you need to set a named property in the PS_INTERNET_HEADERS namespace ({00020386-0000-0000-C000-000000000046}). The code below sets "X-MyHeader" MIME header:

mailMessage.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/string/{00020386-0000-0000-C000-000000000046}/X-MyHeader/0x0000001F", "Custom header value")
0
Eugene Astafiev On

You can add a MIME header to the outgoing email by setting a named property in the PS_INTERNET_HEADERS namespace using PropertyAccessor.SetProperty method:

MailItem.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/string/{00020386-0000-0000-C000-000000000046}/Your-property-name", "YourValue");

On the recipient side you can then read the property on the receiving side by reading the PR_TRANSPORT_MESSAGE_HEADERS (the DASL name is http://schemas.microsoft.com/mapi/proptag/0x007D001F) property using the PropertyAccessor.GetProperty method.