Andrei Smolin

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!

5 Comments

  • https://secure.gravatar.com/avatar/bec191c5490af6281e5628da7d281a55?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Leon Spyropoulos says:

    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

  • https://secure.gravatar.com/avatar/29957f26ad2d8ba527fd9cc8cfa7b2e0?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Andrei Smolin (Add-in Express Team) says:

    Hello Leon,

    I can’t help as this is by design.

  • https://secure.gravatar.com/avatar/741445ce0dd222ba209248169c714b0e?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Lawrence E Hughes says:

    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.

  • https://secure.gravatar.com/avatar/741445ce0dd222ba209248169c714b0e?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Lawrence E Hughes says:

    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.

  • https://secure.gravatar.com/avatar/29957f26ad2d8ba527fd9cc8cfa7b2e0?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Andrei Smolin (Add-in Express Team) says:

    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.

Post a comment

Have any questions? Ask us right now!