Sewder Sewder - 7 months ago 18
HTML Question

Save image on my computer as HTML to embed in email?

I'm trying to convert an image I have on my computer to HTML and embed it into an email:

This is what I've tried so far:

chart1.SaveImage("C:\\My Chart\\mychart.png", ChartImageFormat.Png);


OutlookApp outlookApp = new OutlookApp();
MailItem mailItem = outlookApp.CreateItem(OlItemType.olMailItem);

mailItem.Subject = "This is the subject";
var bytes = File.ReadAllBytes("C:\\My Chart\\mychart.png");
var b64String = Convert.ToBase64String(bytes);
var dataUrl = "data:image/png;base64," + b64String;
mailItem.HTMLBody = dataUrl ;



//Set a high priority to the message
mailItem.Importance = OlImportance.olImportanceHigh;
mailItem.To = "email@email.com";
mailItem.Display(false);


This doesn't work, it shows up as a bunch of code in the actual email

Answer

Building Proper HTML

Since you appear to be sending the body as HTML, try building an <img> tag and setting the src of it to your Base 64 encoded image data :

// Set the body of your email to an image that contains your Base64-encoded image
mailItem.HTMLBody = String.Format("<img src='{0}' />",dataUrl);

This should build your <img> tag as expected, however if will appear or not will be a matter of support.

Checking Browser and Client Support

It's worth noting that while most major browsers will support Base64 URLs, many have issues and limitations. Additionally, some e-mail clients may not support them either as seen below :

enter image description here

Comments