none
how to embed picture in HTMLbody in outlook RRS feed

  • Question

  • hi

    i want to send a html email with images. the image is in my local disk. but after i receive ,the image is not there

    my code is

    .............

    oMailItem.BodyFormat = Microsoft.Office.Interop.Outlook.OlBodyFormat.olFormatRichText;
    oMailItem.HTMLBody = "............<img width="100pt" height="30pt" src="D:\logo.jpg"/>..........."

    is there something wrong in the above code?

    thanks

    Friday, May 28, 2010 8:52 PM

Answers

All replies

  • If you just want to insert the images as attachments, then add the files to the MailItem.Attachments collection.

    But if these images need to be displayed in the message body, then the easiest approach is to reference the images from an http source.  Otherwise, you have to use alternative libraries like CDO or Redemption in order to insert an inline image:

    To add an embedded image to an HTML message in Microsoft Outlook using code:
    http://www.outlookcode.com/d/code/htmlimg.htm


    (P.S. Please don't forget to mark this post as an answer if it has helped you)
    Eric Legault: MVP (Outlook), MCTS (SharePoint)
    Owner: Collaborative Innovations
    Twitter: elegault
    Blog: Eric Legault My Eggo
    • Marked as answer by John83 Zheng Saturday, May 29, 2010 3:37 AM
    Friday, May 28, 2010 9:05 PM
    Moderator
  • Hi Eric,

    I have the similar question hope you can give me your advice.  I successfully embed picture in the message body with CDO codes. I was using MS Outlook 2007.

    Now with MS Outlook 2010, my code doesn't work, and Outlook 2010 does not support CDO. How can I continue  to have picture embeded in the message body with MS Outlook 2010.

    Thank you in advance

     

    Wednesday, January 26, 2011 10:39 AM
  • Use PropertyAccessor (new for Outlook 2007) instead of CDO 1.21.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Wednesday, January 26, 2011 5:42 PM
  • Thanks Dmitry for the quick reply.

    With your helpful answer, I read through the Help and had a better understanding of the PropertyAccessor.

    Now, when I make a picture file as an attachment, which property should I change with propertyAccessor, and with what value.

    If you have sample code for the above, I really appreciate that.

    Thank you

     

     

     


    TS Lim
    Thursday, January 27, 2011 12:43 PM
  • You will nee to set the same properties that your CDO 1.21 code sets using Fields.

    What is your existing CDO 1.21 code?


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Thursday, January 27, 2011 2:07 PM
  • Hi Dmitry,

    Below is the extract of my exisitng code. Appreciate you can help me to have the equivalent using propertyaccessor to set the property value.

          Set objOutlook = CreateObject("Outlook.Application")
          Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
          Set objOutlookRecip = objOutlookMsg.Recipients.Add(!Email1Address)
          objOutlookRecip.Type = olTo
          objOutlookMsg.Subject = "Testing"
          
          ' add "C:\picture.png as attachment to Outlook message
          Set colAttach = objOutlookMsg.Attachments
          Set l_Attach = colAttach.Add("C:\picture.png")
          objOutlookMsg.Close olSave
          strEntryID = objOutlookMsg.EntryID
          Set objOutlookMsg = Nothing
          ' Dereference the  attachment objects before changing their properties via CDO
          Set colAttach = Nothing
          Set l_Attach = Nothing
        
          ' initialize CDO session
          On Error Resume Next
          Set oSession = CreateObject("MAPI.Session")
          oSession.Logon "", "", False, False
     
          ' get the message created earlier
          Set oMsg = oSession.GetMessage(strEntryID)
          ' set properties of the attached C:\picture.png that make it embedded and give it an ID for use in an <IMG> tag
          Set oAttachs = oMsg.Attachments
          Set oAttach = oAttachs.Item(1)
          Set colFields = oAttach.Fields
          Set oField = colFields.Add(CdoPR_ATTACH_MIME_TAG, "image/jpeg")
          Set oField = colFields.Add(&H3712001E, "mypicture")
          oMsg.Fields.Add "{0820060000000000C000000000000046}0x8514", 11, True
          oMsg.Update
     
          ' get the Outlook MailItem again
          Set objOutlookMsg = objOutlook.GetNamespace("MAPI").GetItemFromID(strEntryID)
         
           'Set body format to HTML
           objOutlookMsg.BodyFormat = olFormatHTML
           objOutlookMsg.HTMLBody = "<HTML><head></head><BODY><img src=cid:mypicture</BODY></HTML>"
             
            objOutlookMsg.Send


    TS Lim
    Friday, January 28, 2011 8:41 AM
  • Off the top of my head:

     

          Set objOutlook = CreateObject("Outlook.Application")
          Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
          Set objOutlookRecip = objOutlookMsg.Recipients.Add(!Email1Address)
          objOutlookRecip.Type = olTo
          objOutlookMsg.Subject = "Testing"      
          ' add "C:\picture.png as attachment to Outlook message
          Set colAttach = objOutlookMsg.Attachments
          Set l_Attach = colAttach.Add("C:\picture.png")

          l_Attach.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x370E001E",  "image/jpeg")

          l_Attach.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001E",  "mypicture")

          objOutlookMsg .PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/id/{00062008-0000-0000-C000-000000000046}/8514000B", true)


          objOutlookMsg.Send


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Friday, January 28, 2011 2:38 PM
  • Dear Dmitry,

    After adding the3 lines:

          l_Attach.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x370E001E",  "image/jpeg")

          l_Attach.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001E",  "mypicture")

          objOutlookMsg .PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/id/{00062008-0000-0000-C000-000000000046}/8514000B", true)

    I got the compilation error: Expected:= for all the 3 lines

    Kindly advise.

    Thank you.


    TS Lim
    Friday, January 28, 2011 11:20 PM
  • Try to get rid of parenthesis when calling the SetProperty method.

     


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Friday, January 28, 2011 11:24 PM
  • Did as what you advise. No more compilation error.

    Try sending a mail. My computer do problem to see the embeded picture.

    At the recipient side, picture not embeded in message body, only as an attached file.

    Appreciate you advise again.


    TS Lim
    Saturday, January 29, 2011 12:02 AM
  • Look at the message in the Sent Items folder and Inbox with OutlookSpy (click IMessage). Are these properties correctly set?


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Saturday, January 29, 2011 12:06 AM
  • Not sure with OutlookSpy. Where to click for IMessage ?

    By the way, when the email is sent. I could not see it in the Sent Items folder, but the recipient does recieve the mail. Something abnormal.


    TS Lim
    Saturday, January 29, 2011 12:42 AM
  • Select the message, then click the IMessage button on the OutlookSpy toolbar.

    If Outlook running at the time your code is executed?

    After the line

     Set objOutlook = CreateObject("Outlook.Application")

    try to add

    set NS = objOutlook.GetNamesoace("MAPI")

    NS.Logon


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Saturday, January 29, 2011 1:41 AM
  • After adding

    Set NS = objOutlook.GetNamespace("MAPI")

    NS.Logon.

    and after sending, can see the mail in Send Items folder in my computer, but picture is not embeded, only as attached file.

    Note: Outlook is running when my code is executed.

     


    TS Lim
    Saturday, January 29, 2011 3:50 AM
  • Again, what do you see in OutlookSpy?
    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Saturday, January 29, 2011 4:07 AM
  • Sorry Dmitry, still no clear about how to go to OutlookSpy.

    When you say select the Message in Send Items does it mean the set the sent email in focus ?

    I try to find the IMessage from Outlook but could not ? Need your advise.

     


    TS Lim
    Saturday, January 29, 2011 10:38 AM
  • Did you install OutlookSpy from http://www.dimastr.com/outspy/ ?

     


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Saturday, January 29, 2011 3:56 PM
  • Now I have installed OutlookSpy. Thank you. I now learnt a new tool for checking the property of outlook item!

    When I run with the new code with PropertyAccessor, I could not find the propertïes associate with value "myident" and "image/jpeg" from OutlookSpy.

    When I run the one with my earlier code with CDO, I can find them from OutlookSpy and the DASL are http://schemas.microsoft.com/mapi/proptag/0x3712001F  (for value of myident) and http://schemas.microsoft.com/mapi/proptag/0x370E001F  (for value of image/jpeg)

    So I change the code from 0x3712001E to 0x3712001F amd 0x370E001E to 0x370E001F. But it still does not work.

    Note: I do know where to find the third property.

    Kindly advise. Thank you.


    TS Lim
    Sunday, January 30, 2011 12:13 AM
  • Try to call objOutlookMsg.Save before calling Send.
    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Sunday, January 30, 2011 1:30 AM
  • Tried that, but still not ok.

    Below is my code, hopefully it is helpful for you to spot any mistake.

          Set objOutlook = CreateObject("Outlook.Application")
          Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
          Set objOutlookRecip = objOutlookMsg.Recipients.Add(!Email1Address)
          objOutlookRecip.Type = olTo
          objOutlookMsg.Subject = Forms!EmailAd!EmailSubject
         
          Graphic = Me.AdFile
          ' add graphic as attachment to Outlook message
          Set colAttach = objOutlookMsg.Attachments
          Set l_Attach = colAttach.Add(Graphic)
          Set Ns = objOutlook.GetNamespace("MAPI")
          Ns.Logon

          l_Attach.PropertyAccessor.SetProperty "http://schemas.microsoft.com/mapi/proptag/0x370E001F", "image/jpeg"  'Change From 0x370eE001E

          l_Attach.PropertyAccessor.SetProperty "http://schemas.microsoft.com/mapi/proptag/0x3712001F", "myídent" 'Changed from 0x3712001E

          objOutlookMsg.PropertyAccessor.SetProperty "http://schemas.microsoft.com/mapi/id/{00062008-0000-0000-C000-000000000046}/8514000B", True

           'Set body format to HTML
           objOutlookMsg.BodyFormat = olFormatHTML
           msgHTMLBody = "<HTML" & _
                      "<head>" & _
                      "</head>" & _
                      "<BODY>" & _
                      "        <img align=baseline border=1 hspace=0 src=cid:myident width=""600""" & _
                      "</BODY></HTML>"
             objOutlookMsg.HTMLBody = msgHTMLBody
          objOutlookMsg.Save
          objOutlookMsg.Send


    TS Lim
    Sunday, January 30, 2011 2:17 AM
  • 1. Your HTML is wrong - you need to close the <HTML> tag.

    2. when you set the content id on attachment, you have a wrong "i".

    3. call Namespace.Logon immediately after creating the Outlook.Application object.

    The following script works just fine:

     

          Set objOutlook = CreateObject("Outlook.Application")
          Set Ns = objOutlook.GetNamespace("MAPI")
          Ns.Logon
          Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
          Set objOutlookRecip = objOutlookMsg.Recipients.Add("test@dimastr.com")
          objOutlookRecip.Type = olTo
          objOutlookMsg.Subject = "test"
          ' add graphic as attachment to Outlook message
          Set colAttach = objOutlookMsg.Attachments
          Set l_Attach = colAttach.Add("z:\Temp\8\1.jpg ")
          l_Attach.PropertyAccessor.SetProperty "http://schemas.microsoft.com/mapi/proptag/0x370E001F", "image/jpeg"  'Change From 0x370eE001E
          l_Attach.PropertyAccessor.SetProperty "http://schemas.microsoft.com/mapi/proptag/0x3712001F", "myident" 'Changed from 0x3712001E
          objOutlookMsg.PropertyAccessor.SetProperty "http://schemas.microsoft.com/mapi/id/{00062008-0000-0000-C000-000000000046}/8514000B", True
           'Set body format to HTML
           objOutlookMsg.BodyFormat = olFormatHTML
           msgHTMLBody = "<HTML>" & _
                      "<head>" & _
                      "</head>" & _
                      "<BODY>" & _
                      "        <img align=baseline border=1 hspace=0 src=cid:myident width=""600""" & _
                      "</BODY></HTML>"
             objOutlookMsg.HTMLBody = msgHTMLBody
          objOutlookMsg.Save
          objOutlookMsg.Send


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    • Proposed as answer by TS Lim Monday, January 31, 2011 2:20 AM
    Sunday, January 30, 2011 5:49 PM
  • Dmistry, Thank you for so sharp and able to spot my typo error of the wrong "í" and identity other problem. It works now.

    By the way, can this same propertyaccesor work for MS Outlook 2007 as well ?


    TS Lim
    Monday, January 31, 2011 2:24 AM
  • Sure, PropertyAccessor was introduced in Outlook 2007.
    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    • Proposed as answer by TS Lim Monday, January 31, 2011 7:07 AM
    Monday, January 31, 2011 4:00 AM
  • Probably it doesn't work because you have to use "" instead of " inside the assignment of a String.

    Try this:
    MailItem.HTMLBody = "............<img width=""100pt"" height=""30pt"" src=""D:\logo.jpg""/>..........."
    OR
    MailItem.HTMLBody = "............<img width='100pt' height='30pt' src='D:\logo.jpg'/>..........."
    OR
    MailItem.HTMLBody = "............<img width=100pt height=30pt src='D:\logo.jpg'/>..........."

    Hope this help :-)

    Wednesday, February 8, 2012 12:57 AM