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.

Dale K
  • 25,246
  • 15
  • 42
  • 71
Marcel De Villiers
  • 1,682
  • 4
  • 15
  • 17

2 Answers2

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>";
jgauffin
  • 99,844
  • 45
  • 235
  • 372
Jagan
  • 665
  • 6
  • 5
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

Community
  • 1
  • 1
Dragos Bobolea
  • 772
  • 7
  • 16
  • 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 – Marcel De Villiers May 22 '13 at 07:27