views:

490

answers:

4

Hello there,

I am sending Html document(containing Images) as an email attachment at run-time using C#.
But when I check the email received, html document that was sent doesn't contain any image.

Can you please guide how can I make sure that html documents are sent along with images.

        MailMessage objMailMessage = new MailMessage();
        objMailMessage.From = new MailAddress("[email protected]");

        string[] emailIds = objReportRequest.EmailIds.Split(',');
        foreach (string emailId in emailIds)
        {
            objMailMessage.To.Add(new MailAddress(emailId));
        }

        objMailMessage.IsBodyHtml = true; 
        objMailMessage.Body = messageBody;
        objMailMessage.Subject = "Test Service";
        objMailMessage.Attachments.Add(new Attachment(filePath));

        SmtpClient objSmtpClient = new SmtpClient("smtp.gmail.com");
        objSmtpClient.Credentials = new NetworkCredential("[email protected]", "aaa");
        objSmtpClient.EnableSsl = true;
        objSmtpClient.Send(objMailMessage);

I am receiving the html doc as an email attachment but images are not displayed.

Thank you!

+1  A: 
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(message, null, "text/html");
 LinkedResource image = new LinkedResource(@".\images\sample.jpg");
 image.ContentId = "Image";
 message = "<img src=cid:Image>" + message;
 htmlView = AlternateView.CreateAlternateViewFromString(message, null, "text/html");
 htmlView.LinkedResources.Add(image);

Is how you would put an image into an HTML email, html attachments according to MSDN use the same objects, but I dont have code to do that specifically.

Alex
+3  A: 

This is a System.Net.Mail FAQ.

Wim Coenen
A: 

If the HTML is sent as an attachment, I don't think you can embed images in it. HTML is text, after all.

If the image is static, I think your best bet is to reference the image through an absolute path, and host the image on one of your servers.

bryanjonker
The HTML can contain links to other attachments, such as images. The "content-id" URL format that is used to reference other attachments is described in RFC 2111 http://www.mhonarc.org/~ehood/MIME/rfc2111.txt
Wim Coenen
A: 

Hi, Since you are sending the HTML as an attachment and not as a body, there isn't an easy way to do this.

Here are a few options:

a) The best way is to actually build a MHT (or MHTML) document, and send that as an attachment.

b)Instead of adding the HTML as an attachment, you may want to populate the email body with the HTML, and then embed the images.

c)Another option is to simply build an email, embed the images in that email, and add the email as an attachment.

d)If the recipient will be using an updated browser, you can embed the images directly in the tag by using the data URL scheme. http://en.wikipedia.org/wiki/Data_URI_scheme

e)Like another poster suggested, use absolute links in the tags, and host the images somewhere.

Cheers! Dave

dave wanta