none
Outlook 2010 not displaying html image RRS feed

  • Question

  • Hi,

    I am sending mails via C# asp.net windows service (using SMTPClient) code. 

    The body of the email is sent as HTML and it contains an image too. The 

    image is a base64 encoded image.
    The html generated can be viewed properly on IE 7.0/8.0/Compatibility mode 

    but outlook displays only the body text. The image is replaced by red X and 

    says that the linked image cannot be displayed.The file may have been 

    moved, renamed or have been deleted. Verify that the link points to the 

    correct file and location.
    If I give the physical/relative path of the image via the code, the mail 

    goes to the junk folder. Please suggest any workaround.

    Thanks in advance,
    Richa.

    Regards, Richa

    Tuesday, September 4, 2012 4:17 PM

Answers

  • I do not use the MailMessage class, but you would need to use the MailMessage.Attachments collection to add the image and set the Content-ID MIME header on the attachment.

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.3 is now available!

    • Marked as answer by richa_del Thursday, September 6, 2012 4:39 PM
    Thursday, September 6, 2012 2:07 AM

All replies

  • How does the HTML body refer to the embedded image attachment?

    What is the <img> tag?


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.3 is now available!

    Tuesday, September 4, 2012 4:34 PM
  • The body of the email is passed as an html string. Within the string, image tag is placed. The image is not passed as an attachment, it has to be the part of the email body.

    Regards, Richa

    Tuesday, September 4, 2012 4:38 PM
  • How can it be a part of the HTML body? Do you stuff the base64 encoded image data inside the IMG tag?


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.3 is now available!

    Tuesday, September 4, 2012 5:01 PM
  • ye, the image tag is similar to the following text:-

    <img alt="my Logo" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAK8AAAA0CAIAAABzUf&#43 ....

    The html text, when viewed on IE displays the image properly but not on outlook/ webmail. 


    Richa

    Tuesday, September 4, 2012 5:16 PM
  • Outlook uses Word to render HTML, and I don't think it supports embedded images in HTML.

    Try to add the images as separate attachments and refer to them through the content id header: <img cid:xyz>, and set the Content-id MIME header of the attachment appropriately.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.3 is now available!

    Tuesday, September 4, 2012 5:28 PM
  • Can you please guide how to associate the cid of an image with the image? Since I cannot give the physical/relative path, I can only think of giving the base64 encoded string. How do I associate this string to cid? Or is there any other way that I can assign the image to cid? 

    Richa

    Tuesday, September 4, 2012 6:04 PM
  • You need to generate your own unique (within the mesage) content id.

    The actual image data must be in a separate MIME part (attachment).


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.3 is now available!

    Tuesday, September 4, 2012 8:36 PM
  • Can you please let me know how do I make the actual image data a part of MIME part.

    I  do not want the image to come as an attachment, it should be a part of the mail body.


    Richa

    Wednesday, September 5, 2012 6:13 AM
  • I asume you build the MIME message? Image needs to be added as an attachment - that will make it a separate MIME part in the message.

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.3 is now available!

    Wednesday, September 5, 2012 6:27 AM
  • My message body is html string.

    The mail is sent as:-

     using (MailMessage message = new MailMessage(mailFrom, toEmail, subjectofmail, bodyofmail))
                        {
                            message.IsBodyHtml = true;
                            client.Send(message);
                        }

    The bodyofmail is a html string in which the image and the text is passed.

    How can I convert the message to MIME type and how shall I make the base64 encoded image as MIME type? 


    Richa

    Wednesday, September 5, 2012 8:56 AM
  • I do not use the MailMessage class, but you would need to use the MailMessage.Attachments collection to add the image and set the Content-ID MIME header on the attachment.

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.3 is now available!

    • Marked as answer by richa_del Thursday, September 6, 2012 4:39 PM
    Thursday, September 6, 2012 2:07 AM
  • Hi, I went through the complete conversation, even i was facing the same issue in my project.

    Below are updates which worked for me.

    1. If u get the image source location from remote server in the src attribute.

    2. Instead of converting to "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAK8AAAA0CAIAAABzUf&#43" convert to byte array(byte[] image2Bytes).

    3. After converting to byte array, follow below steps.

    string emailReadyHtml = string.empty; emailReadyHtml += "<p>Hello World, below are two embedded images : </p>"; emailReadyHtml += "<img src=\"cid:smile\" >"; ContentType c = new ContentType("image/jpeg"); // the linked resource can be created from bytes also, which may be stored in database (which was my case)

    LinkedResource linkedResource2 = new LinkedResource(new MemoryStream(image2Bytes));

    linkedResource2.ContentType = c; linkedResource2.ContentId = "smile"; linkedResource2.TransferEncoding = TransferEncoding.Base64; AlternateView alternativeView = AlternateView.CreateAlternateViewFromString(emailReadyHtml, null, MediaTypeNames.Text.Html); alternativeView.ContentId = "htmlView"; alternativeView.TransferEncoding = TransferEncoding.SevenBit; alternativeView.LinkedResources.Add(linkedResource2); mailMessage.AlternateViews.Add(alternativeView); SmtpClient smtpClient = new SmtpClient(); smtpClient.Send(mailMessage);



    4. It worked completly fine in outlook2013.

    5. refer the link below where i got the solution.

    http://stackoverflow.com/questions/16737430/images-breaking-when-sending-mail-using-smtpclient

    (Thanks to stackoverflow.com and social.msdn.microsoft.com)



    Friday, June 3, 2016 11:30 AM