How to get and set the format of an Outlook email message
Outlook supports creating emails in plain-text, HTML and RTF (rich-text) formats. To get or set the format of an email message, you use the MailItem.BodyFormat property; it isn’t available in Outlook 2000. The value returned by the property is one of the Outlook.OlBodyFormat constants.
Outlook.OlBodyFormat constant | Description |
olFormatUnspecified | Unspecified format |
olFormatPlain | Plain format |
olFormatHTML | HTML format |
olFormatRichText | Rich text format (RTF) |
Other Outlook objects providing the BodyFormat property are SharingItem (Outlook 2010 only) and PostItem.
Accessing that property via early binding is straightforward. Below is an example of accessing it via late binding. Note that changing the BodyFormat property may result in losing all formatting of the message.
VB.NET
Sub SetMailFormat_2002_2003_2007_2010(ByRef mail As Object) Dim mailFormat As System.Int32 mailFormat = Convert.ToInt32(mail.GetType().InvokeMember("BodyFormat", _ Reflection.BindingFlags.GetProperty, Nothing, mail, Nothing)) 'OlBodyFormat.olFormatUnspecified = 0 'OlBodyFormat.olFormatPlain = 1 'OlBodyFormat.olFormatHTML = 2 'OlBodyFormat.olFormatRichText = 3 If (mailFormat = 1) Then mailFormat = 2 mail.GetType().InvokeMember("BodyFormat", _ Reflection.BindingFlags.SetProperty, _ Nothing, mail, New Object() {mailFormat}) End Sub
C#
void SetMailFormat_2002_2003_2007_2010(object mail) { System.Int32 mailFormat; mailFormat = Convert.ToInt32(mail.GetType().InvokeMember("BodyFormat", System.Reflection.BindingFlags.GetProperty, null, mail, null)); //OlBodyFormat.olFormatUnspecified = 0 //OlBodyFormat.olFormatPlain = 1 //OlBodyFormat.olFormatHTML = 2 //OlBodyFormat.olFormatRichText = 3 if (mailFormat == 1) mailFormat = 2; mail.GetType().InvokeMember("BodyFormat", System.Reflection.BindingFlags.SetProperty, null, mail, new object[1] { mailFormat }); }
Good luck!
8 Comments
Hello,
You state that “Note that changing the BodyFormat property may result in losing all formatting of the message”.
I read a rich text outlook item and in the object the body defaults to HTML. When I BodyFormat to RTF I lose all format. Can you please help me.
Thank you,
Leon
Hello Leon,
I can’t help as this is by design.
Hi,
I am setting the BodyFormat to RTF and still the html body is populated. Also, reason why I am trying to send RTF format mail is to have custom properties in the mailitem to be sent over to the recipient.
When I receive the mail even after setting the custom properties, it is not found in the recipient mail box. Can you please tell me what I am missing?
FYI, I have set below properties:
SetProperty(“MyCustomProperty”, value, mailItem);//Setting the custom property
mailItem.PropertyAccessor.SetProperty(BlackbirdConstants.SendCustomProerty, true);//Setting the TNEF to true
mailItem.BodyFormat = Microsoft.Office.Interop.Outlook.OlBodyFormat.olFormatRichText;
After receiving the email, I dont see my custom property. Also, there is no Winmail.dat file attached. Can you please help me on this?
Please let me know if you need any other info.
Thanks,
Adi.
Hi,
I am setting the BodyFormat to RTF and still the html body is populated. Also, reason why I am trying to send RTF format mail is to have custom properties in the mailitem to be sent over to the recipient.
When I receive the mail even after setting the custom properties, it is not found in the recipient mail box. Can you please tell me what I am missing?
FYI, I have set below properties:
SetProperty(“MyCustomProperty”, value, mailItem);//Setting the custom property
mailItem.PropertyAccessor.SetProperty(BlackbirdConstants.SendCustomProerty, true);//Setting the TNEF to true
mailItem.BodyFormat = Microsoft.Office.Interop.Outlook.OlBodyFormat.olFormatRichText;
After receiving the email, I dont see my custom property. Also, there is no Winmail.dat file attached. Can you please help me on this?
Please let me know if you need any other info.
Thanks,
Lawrence.
Hello Lawrence,
On the custom properties issue, please find an explanation at https://support.microsoft.com/en-us/help/907985/changes-to-custom-properties-in-outlook.
As to the HTMLBody property, I suppose Outlook keeps it in sync with the other properties such as Body.
for what ever reason … the .body value can be set to anything, like rtf. using .rtfbody blows up. use the clipboard.
Weirdly, when running “mail.GetType().InvokeMember(“BodyFormat”,
System.Reflection.BindingFlags.SetProperty, null, mail,
new object[1] { mailFormat }); ”
on an e-mail saved as a downloaded .msg file (not existing in user’s account), it SAVES the e-mail to the user’s Inbox.
How strange? Anyone else encountered this, or know of a way to prevent it doing so?
Users are downloading messages from another system, opening them up and running a .vsto addin to forward the message on with some automation / formatting. As part of this it enforces mailFormat = 2 (HTML), and on hitting that code above, the original message then gets saved to the user’s Inbox, and they don’t want it to be…
This is strange, indeed. But it is Outlook who does the save so you can’t avoid this issue unless you stop changing that property. Also, there’s a small chance this is an Outlook issue that will be fixed in some Outlook build.