Interop.Outlook 消息以 HTML 形式显示为纯文本

问题描述 投票:0回答:1

我最近从使用 EWS 切换到使用 Interop.Outlook(请参阅本文)。该过程非常容易使用!

不幸的是,我遇到了一个 EWS 中不存在的问题:即使 BodyFormat 设置为 true,Outlook 也不会处理 HTML 正文。在此代码示例 (VB.NET) 中,MessageBody 确实以 < 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)

使用 EWS 时可以正确显示完全相同的正文。

html email outlook office-interop
1个回答
2
投票
  .Body = MessageBody
MailItem 类的

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)
    
© www.soinside.com 2019 - 2024. All rights reserved.