htmlemailoutlookoffice-interop

Interop.Outlook Message in HTML displayed as Plain Text


I recently switched from using EWS to using Interop.Outlook (see this article). The process is extremely easy to use!

Unfortunately, I have one problem that did not exist in EWS: Outlook does not process the HTML body even when BodyFormat is set to true. In this code sample (VB.NET), the MessageBody does start with < HTML. With debug, I verified that BodyFormat was set to HTML when display was executed. Nevertheless, the email body is displayed as plain text.

Dim Outlook As New Outlook.Application
Dim mail As Outlook.MailItem =  DirectCast(Outlook.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem), Outlook.MailItem)

With mail
    .To = Addr
    .Subject = Subject
    .Body = MessageBody
    .BodyFormat = If(MessageBody.ToLower.StartsWith("<html"),
        Microsoft.Office.Interop.Outlook.OlBodyFormat.olFormatHTML,
        Microsoft.Office.Interop.Outlook.OlBodyFormat.olFormatPlain)
    .Display(Modal)

The exact same body text, when using EWS displays correctly.


Solution

  •   .Body = MessageBody
    

    The Body property of the MailItem class is a string representing the clear-text body of the Outlook item (without formatting). You need to set the body format first (if required). By default Outlook uses the HTML format.

    With mail
    .To = Addr
    .Subject = Subject
    If(MessageBody.ToLower.StartsWith("<html")) Then
      .BodyFormat = Microsoft.Office.Interop.Outlook.OlBodyFormat.olFormatHTML
      .HTMLBody = MessageBody
    Else
      .BodyFormat = Microsoft.Office.Interop.Outlook.OlBodyFormat.olFormatPlain
      .Body = MessageBody
    End If
    .Display(Modal)
    

    Use the HTMLBody property for setting an HTML markup.

    Or just simply:

     With mail
    .To = Addr
    .Subject = Subject
    If(MessageBody.ToLower.StartsWith("<html")) Then      
      .HTMLBody = MessageBody
    Else
      .Body = MessageBody
    End If
    .Display(Modal)