Embedded image when export report to outlook mail as html

494 Views Asked by At

I have this code that export my report to html then show it on outlook body

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 = "[email protected]";
    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;

}

All things go well except for images does not show in my outlook body. I expect this:

enter image description here

but I get this:

enter image description here

I tried to use

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

but it seems to work with xrPictureBox only and I use xrCheckBox and all image are built-in within controls themselves

1

There are 1 best solutions below

1
On

Outlook supports HTML images either as external links to files on a web server or as references to an attachment - in the latter case, the HTML body references the image attachment through the src=cid:xyz attribute of the img tab, where "xyz" is the value of the PR_ATTACH_CONTENT_ID property on the attachment. See How to add images from resources folder as attachment and embed into outlook mail body in C# for more details.

Note that Outlook does not support base64-encoded embedded images - this is a Word limitation (Word renders HTML messages in Outlook).