Interop.Outlook HTML 中的消息显示为纯文本

Interop.Outlook Message in HTML displayed as Plain Text

我最近从使用 EWS 切换到使用 Interop.Outlook ()。该过程非常易于使用!

不幸的是,我遇到了一个 EWS 中不存在的问题:即使 BodyFormat 设置为 true,Outlook 也不会处理 HTML 正文。在此代码示例 (VB.NET) 中,MessageBody 以 < HTML 开头。通过调试,我验证了在执行显示时 BodyFormat 已设置为 HTML。然而,电子邮件正文显示为纯文本。

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)

完全相同的正文,在使用 EWS 时正确显示。

  .Body = MessageBody

MailItem class 的 Body 属性 是一个字符串,表示 Outlook 项目的明文正文(无格式)。您需要先设置正文格式(如果需要)。默认情况下,Outlook 使用 HTML 格式。

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)

使用 HTMLBody 属性 设置 HTML 标记。

或者简单地说:

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