Marcel De Villiers
Marcel De Villiers

Reputation: 1832

Email HTML document, embedding images using c#

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.

Upvotes: 5

Views: 29495

Answers (2)

Jagan
Jagan

Reputation: 665

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>";

Upvotes: 42

Dragos Bobolea
Dragos Bobolea

Reputation: 782

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

Upvotes: 2

Related Questions