Monday, October 27, 2008

Hotmail Embedded Images in HTML email not working

With one of my recent projects going live I experienced an issue with hotmail email client not showing the emails sent by my application correctly. Especially embedded images in the email. These images were replace by a gray box "i_safe.gif" making the email look bad. This was only happening in hotmail. Again Microsoft had to be special.
Gmail, Yahoo, Exchange Outlook etc was rendering the email perfectly fine.

Hotmail just need to have additional information in the email LinkedResource object.

Setting the properties LinkedResource.ContentType.Name and LinkedResource.TransferEncodig fixed this problem.

backImageResource.ContentType.Name = "dress_back";

backImageResource.TransferEncoding = TransferEncoding.Base64;
Here is my code example that work the emails working.








// Create the html AlternateView

AlternateView htmlView = AlternateView.CreateAlternateViewFromString(template.Body, null, "text/html");

// Add Linked Resources for the embedded images

LinkedResource frontImageResource = new LinkedResource(Utility.GetImageStream(DressFrontView, ImageFormat.Jpeg), MediaTypeNames.Image.Jpeg);

frontImageResource.ContentId = "dress_front";

frontImageResource.ContentType.Name = "dress_front";

frontImageResource.TransferEncoding = TransferEncoding.Base64;



LinkedResource backImageResource = new LinkedResource(Utility.GetImageStream(DressBackView, ImageFormat.Jpeg), MediaTypeNames.Image.Jpeg);

backImageResource.ContentId = "dress_back";

backImageResource.ContentType.Name = "dress_back";

backImageResource.TransferEncoding = TransferEncoding.Base64;





// Relate resources with AlternativeView

htmlView.LinkedResources.Add(frontImageResource);

htmlView.LinkedResources.Add(backImageResource);






After adding the ContentType.Name and TransferEncoding the e-mails displayed corrently in hotmail.

No comments:

Post a Comment