How to resolve issues with embedded images in email.

May 10th, 2012 by admin Leave a reply »

I am embedding an image into an email that the server is generating using the SmtpClient in .net and I was having an issue with it caching images in certain situations and then Internet Explorer wouldn’t display the images at all, which is probably a security feature of IE, but the images did show up in Firefox and Chrome. Anyways, here is the code below that will make all of it work correctly in IE and should always show the images correctly, it generates a new Guid to always have a unique image Id so that caching won’t happen and then also tells the client that the ContentType is a jpeg which apparently IE needs to display the image correctly.

Guid contentId = Guid.NewGuid().ToString();

AlternateView htmlView = AlternateView.CreateAlternateViewFromString(
  "This is a sample JPG embedded image<br><img src="cid:" + contentId + "">", 
  null, "text/html");

ContentType ct = new ContentType(MediaTypeNames.Image.Jpeg);

LinkedResource EmbeddedObjects1 = new LinkedResource("PathToImage\image1.jpg", ct);
EmbeddedObjects1.ContentId = contentId;
htmlView.LinkedResources.Add(EmbeddedObjects1);

Advertisement

Leave a Reply