Email HTML document embedding images using C #

So here is my situation.

I need to send an email as an HTML string with images embedded in it. I tried to convert my images to base64, but it does not work.

There are 3 images in the email type System.Drawing.Image. I just need to get them in my html formatted string.

+4
source share
2 answers

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

Check Bukhake’s answer, it very well describes the problem as a whole (you should be able to get it from there): How to embed images in email

+2
source

Another way to embed images in E-mail when using System.Net.Mail is to attach the image from the local drive to the email and assign it a contentID , and then use that contentID in the image URL.

This can be done as follows:

 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>"; 
+38
source

Source: https://habr.com/ru/post/1479746/


All Articles