1

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

Ani Menon
  • 27,209
  • 16
  • 105
  • 126
Sewder
  • 754
  • 3
  • 11
  • 37
  • 1
    Lots of email clients wont display inline base64 images, last I looked that included Outlook – Alex K. Apr 29 '16 at 16:22
  • Take a look at this answer from another question: [link](http://stackoverflow.com/a/9110164/6220820) It looks like you need to embed it as an attachment. – mhagrelius Apr 29 '16 at 16:24
  • @AlexK. That explains why this isn't working now :( – Sewder Apr 29 '16 at 16:27
  • Possible duplicate of [base64 encoded images in email signatures](http://stackoverflow.com/questions/9110091/base64-encoded-images-in-email-signatures) – Heretic Monkey Apr 29 '16 at 16:34
  • Using `DataUri` is not recommended because of weak support in most of mail clients, I tested it with *Outlook.com(web)* and *OutlookWebAccess(web)* and *Office Outlook(Windows)* and *Outlook(windows 8.1)* and unfortunately it worked only on *OutlookWebAccess(web)* Instead I recommend you using attachment contentid. For more information read [this post](http://stackoverflow.com/questions/32767314/how-to-use-razor-engine-for-email-templating-with-image-src). – Reza Aghaei May 11 '16 at 13:30

1 Answers1

4

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

Rion Williams
  • 74,820
  • 37
  • 200
  • 327
  • This gets an image into the email, but it's a red x with the error message. can't load image. Maybe I didn't save the image correctly? – Sewder Apr 29 '16 at 16:21
  • That's likely a matter of support. I've provided an example that demonstrates that Outlook 2007+ may natively block images from appearing in such a matter. You may want to consider an approach similar to [the link provided in Vortex's comment](http://stackoverflow.com/a/9110164/6220820) on another approach for handling this. – Rion Williams Apr 29 '16 at 16:29