开发者

Send Html document(contianing Images) as an email attachment C#

开发者 https://www.devze.com 2022-12-14 19:58 出处:网络
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.

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("aa@gmail.com");

        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("aaa@gmail.com", "aaa");
        objSmtpClient.EnableSsl = true;
        objSmtpClient.Send(objMailMessage);

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

Thank you!


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.


This is a System.Net.Mail FAQ.


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.


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:

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

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

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

  4. 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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号