Problems with embedding images in system.net.mail.mailmessage
-
Hello! Some of the images I try to embedd do not show up in my email, but are instead sent as attachments. I can´t figure out why some are working and some arent. This is how I embedd images:
MailMessage mail = new MailMessage(); string imagePath = System.Windows.Forms.Application.StartupPath + "\\pathToImages"; LinkedResource resource1 = new LinkedResource(imagePath + "\\head21.jpg", System.Net.Mime.MediaTypeNames.Image.Jpeg); resource1.ContentId = "head21.jpg"; LinkedResource resource2 = new LinkedResource(imagePath + "\\border.jpg", System.Net.Mime.MediaTypeNames.Image.Jpeg); resource2.ContentId = "border.jpg"; StringBuilder content = new StringBuilder(); .... content.Append(string.Format("<td width=600 align=left><img border=0 align=center src=\"cid:head21.jpg\" width=600 height=57></td></tr></tbody></table></td></tr></tbody></table>")); .. content.Append(string.Format("<td><img alt=\"\" src=\"cid:border.jpg\" width=560 height=21></td></tr></tbody></table></td>")); .. string body = Mail.CreateHtmlFromTemplate(content.ToString(), string.Empty, null); AlternateView htmlView = AlternateView.CreateAlternateViewFromString(body, null, MediaTypeNames.Text.Html); htmlView.LinkedResources.Add(resource1); htmlView.LinkedResources.Add(resource2);
Here resource1 works fine, and shows up in body of email. Resource2 is instead sent as attachment and does not show up in the email. Does anyone have any idea why this is? -
Hello! Some of the images I try to embedd do not show up in my email, but are instead sent as attachments. I can´t figure out why some are working and some arent. This is how I embedd images:
MailMessage mail = new MailMessage(); string imagePath = System.Windows.Forms.Application.StartupPath + "\\pathToImages"; LinkedResource resource1 = new LinkedResource(imagePath + "\\head21.jpg", System.Net.Mime.MediaTypeNames.Image.Jpeg); resource1.ContentId = "head21.jpg"; LinkedResource resource2 = new LinkedResource(imagePath + "\\border.jpg", System.Net.Mime.MediaTypeNames.Image.Jpeg); resource2.ContentId = "border.jpg"; StringBuilder content = new StringBuilder(); .... content.Append(string.Format("<td width=600 align=left><img border=0 align=center src=\"cid:head21.jpg\" width=600 height=57></td></tr></tbody></table></td></tr></tbody></table>")); .. content.Append(string.Format("<td><img alt=\"\" src=\"cid:border.jpg\" width=560 height=21></td></tr></tbody></table></td>")); .. string body = Mail.CreateHtmlFromTemplate(content.ToString(), string.Empty, null); AlternateView htmlView = AlternateView.CreateAlternateViewFromString(body, null, MediaTypeNames.Text.Html); htmlView.LinkedResources.Add(resource1); htmlView.LinkedResources.Add(resource2);
Here resource1 works fine, and shows up in body of email. Resource2 is instead sent as attachment and does not show up in the email. Does anyone have any idea why this is?Neat, you can do that? :) My guess is that it may have something to do with the html? How about if you don't do any of the table stuff, and just put the images inline? Does that work?? I've always found coding tables in HTML as tricky. It's easy to miss something. And, depending on the browser, the browser may or may not "fix" some html coding errors. __________ Joe
-
Neat, you can do that? :) My guess is that it may have something to do with the html? How about if you don't do any of the table stuff, and just put the images inline? Does that work?? I've always found coding tables in HTML as tricky. It's easy to miss something. And, depending on the browser, the browser may or may not "fix" some html coding errors. __________ Joe
Thanks for your response! I tried just having the faulty image in a div instead, but it didn´t work :( EDIT: My mistake u were absolutely right. There wasnt enough room for the image, thats why it was sent as an attachment. Thanks alot!
modified on Tuesday, August 18, 2009 4:37 AM
-
Thanks for your response! I tried just having the faulty image in a div instead, but it didn´t work :( EDIT: My mistake u were absolutely right. There wasnt enough room for the image, thats why it was sent as an attachment. Thanks alot!
modified on Tuesday, August 18, 2009 4:37 AM