5

I need to send an email as a string in HTML format that has images embedded in it. I tried converting my images into base64 but it does not work.

The email has 3 images as type System.Drawing.Image. I just need to get them in my HTML formatted string.

4
  • I believe you'd be able to use the base64 string as the src attribute of an img tag. What have you tried, specifically? Commented May 8, 2013 at 13:40
  • Partly true: it could work with src="data:image/png;base64,YOUR_BASE_64_ENCODED_DATA", but it's not universally supported. Commented May 8, 2013 at 13:47
  • possible duplicate of How to embed images in email Commented May 9, 2013 at 7:17
  • possible duplicate of embedding image in html email Commented May 6, 2014 at 7:06

2 Answers 2

42

The other way to embed images in E-mail when using System.Net.Mail is to attach image from local drive to email and assign a contentID to it and later use this contentID in the image URL.

That can be done like this:

msg.IsBodyHtml = true;
Attachment inlineLogo = new Attachment(@"C:\Desktop\Image.jpg");
msg.Attachments.Add(inlineLogo);
string contentID = "Image";
inlineLogo.ContentId = contentID;

//To make the image display as inline and not as attachment

inlineLogo.ContentDisposition.Inline = true;
inlineLogo.ContentDisposition.DispositionType = DispositionTypeNames.Inline;

//To embed image in email

msg.Body = "<htm><body> <img src=\"cid:" + contentID + "\"> </body></html>";
Sign up to request clarification or add additional context in comments.

3 Comments

+1 Very nice solution. I can now do something else for the rest of my day. ;)
Saved my day, cleanest solution that worked for me +1
Excellent. Thx a lot :)
2

You were right about converting to base64, but it is not enough to embed it (there would be no way for the client to distinguish base64 to plain text), you need to format it a bit.

Check out Buhake's answer, it covers very well the problem in general (you should be able to take it from there): How to embed images in email

1 Comment

Base64 is a good solution, but not for my app since high quality images were to be sent. I took a quick look, sending a single image would result in a 2300 page text file. I solved the problem by not embedding them in the html email itself. I simply sent it as attachments and referenced it in html dynamically

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.