C# VSTO Add-in - Convert plain text email to HTML

荒凉一梦 提交于 2019-12-10 17:50:14

问题


We have written a VSTO addin for Outlook 2010/2007.

At one point, our addin must convert plain text emails to HTML from a ribbon control. This causes some strange behavior in Outlook:

  1. We're using the MailItem COM object
  2. We set MailItem.HTMLBody
  3. Email is converted to HTML, but for some reason the font is Times New Roman at 10pt

The default font in Outlook is Calibri 11pt, which makes our email conversion look pretty strange to the user. It works as expected when you use the existing button in Outlook to convert to HTML, but not when using our addin's button.

So we tried the following:

  • Set MailItem.BodyFormat ahead of time
  • Wrapped our email text with: <span style='font-size:11.0pt;font-family:"Calibri","sans-serif"'></span> (We got this idea from viewing the source of a new Outlook email)

Wrapping the <span> tag around the email body worked as far as changing the font to Calibri, but the font size stayed at 10pt...

Is there a better way to do this? Another workaround?

EDIT, working code:

        if (_mailItem.BodyFormat != OlBodyFormat.olFormatHTML)
        {
            _mailItem.GetInspector.CommandBars.ExecuteMso("MessageFormatHtml");
        }

_mailItem is a Microsoft.Office.Interop.Outlook.MailItem.


回答1:


You have two ways:

  1. "Click" the built-in button programmatically via Inspector.CommandBars.ExecuteMso("MessageFormatHtml")
  2. Close the inspector, convert the message format, save the email and re-open it.


来源:https://stackoverflow.com/questions/6124865/c-sharp-vsto-add-in-convert-plain-text-email-to-html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!