2

I am trying to attach an image into a html template used to send emails. The code I am trying to make it work looks like this:

string body = System.IO.File.ReadAllText("./Resources/templates/myTemplate.html");
byte[] imageArray = System.IO.File.ReadAllBytes(@"logo.jpg");
string base64ImageRepresentation = Convert.ToBase64String(imageArray);

body = body.Replace("{imageBase64}", base64ImageRepresentation);

and the myTemplate.html code is:

<html>
<head></head>
   <body>
     <p>This is the email text</p>
     <img src={imageBase64}/>
   </body>
</html>

But the email is not being sent. If I remove the code related to the image the email is sent correctly.

Does anyone have an idea how to solve it?

Steve
  • 29
  • 1
  • 8
  • Have you seen this [related question](https://stackoverflow.com/questions/8499633/how-to-display-base64-images-in-html)? – Axel Kemper Sep 17 '20 at 21:53

1 Answers1

0

format of the src needs to be

src="data:image/png;base64, {imageBase64}"

if this does not work, consider adding character set to the image src

src="data:image/png;charset=utf-8;base64, {imageBase64}"
Derviş Kayımbaşıoğlu
  • 28,492
  • 4
  • 50
  • 72