C# Email embedded image does not display in IE
I am using C#.Net to send out a HTML email which contains embedded images. These emails work fine in Firefox and chrome but the images do not show up in Internet Explorer (IE). I know that the IE settings are not caus开发者_JAVA技巧ing the problem since embedded images sent using Blat work just fine. Am I missing some option such as character set which is causing this problem? My code is as follows
MailMessage msg = new MailMessage();
MailAddress from = new MailAddress("Myemail@MyDomain", "My Name");
msg.To.Add("Myemail@MyDomain");
msg.From = from;
msg.Subject = "My subjecct line";
AlternateView htmlView = AlternateView.CreateAlternateViewFromString("This is a sample JPG embedded image<br><img src=\"cid:image1.jpg\">", null, "text/html");
LinkedResource EmbeddedObjects1 = new LinkedResource("PathToImage\\image1.jpg");
EmbeddedObjects1.ContentId = "image1.jpg";
htmlView.LinkedResources.Add(EmbeddedObjects1);
msg.AlternateViews.Add(htmlView);
SmtpClient smtpclient = new SmtpClient("mailhost.domain.com", PortNumber);
smtpclient.Send(msg);
Maybe it helps if you create the LinkedResource with a ContentType combined with Coding Gorilla's idea of a guid as the content-id:
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);
Try adding the following.
msg.IsBodyHtml = true;
Additionally, I usually setup my AlternateView like this.
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(html, new ContentType("text/html"));
Embedded images work by creating dataUri
schemes. IE < 8 doesn't support those.
精彩评论