المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Problems with embedding images in system.net.mail.mailmessage



C# Programming
08-17-2009, 06:41 PM
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("<img border=0 align=center src=\"cid:head21.jpg\" width=600 height=57>"));

..

content.Append(string.Format("<img alt=\"\" src=\"cid:border.jpg\" width=560 height=21>"));

..

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?