首页 > 解决方案 > 将报告作为 html 导出到 Outlook 邮件时的嵌入图像

问题描述

我有这段代码可以将我的报告导出为 html,然后在 Outlook 正文中显示

Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook.MailItem oMsg = (Microsoft.Office.Interop.Outlook.MailItem)oApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);


using (RepProjectInfoCH report = new RepProjectInfoCH())
{
    report.DataSource = await ProjInfo.RepProjectInfoCH(idProject).ConfigureAwait(true);

    string str;
    MemoryStream ms = new MemoryStream();
    try
    {

        report.ExportToHtml(ms);
        ms.Seek(0, SeekOrigin.Begin);
        using (StreamReader sr = new StreamReader(ms))
        {
            str = sr.ReadToEnd();
        }
    }
    finally
    {
        ms.Close();
    }

    oMsg.To = "Test@Test.com";
    oMsg.Subject = "Test" ;
    oMsg.BodyFormat = Microsoft.Office.Interop.Outlook.OlBodyFormat.olFormatHTML;
    oMsg.Display(false); //In order to display it in modal inspector change the argument to true
    oMsg.HTMLBody = str + oMsg.HTMLBody; //Here comes your body;

}

除了图像没有显示在我的 Outlook 正文中之外,一切都很顺利。我期望这个:

在此处输入图像描述

但我明白了:

在此处输入图像描述

我试着用

HtmlExportOptions htmlOptions = report.ExportOptions.Html;
htmlOptions.EmbedImagesInHTML = true;

但它似乎只适用于 xrPictureBox 并且我使用 xrCheckBox 并且所有图像都内置在控件本身中

标签: c#winformsoutlookxtrareportexport-to-html

解决方案


Outlook 支持将 HTML 图像作为指向 Web 服务器上文件的外部链接或作为对附件的引用 - 在后一种情况下,HTML 正文通过选项卡的src=cid:xyz属性引用图像附件img,其中“xyz”是PR_ATTACH_CONTENT_ID属性的值在附件上。有关详细信息,请参阅如何将资源文件夹中的图像作为附件添加并嵌入到 C#中的 Outlook 邮件正文中。

请注意,Outlook 不支持 base64 编码的嵌入图像 - 这是 Word 限制(Word 在 Outlook 中呈现 HTML 消息)。


推荐阅读