none
Display a picture in HTML format RRS feed

  • Question

  • I have two questions.  One how do you get the Recipient email to work.  Two how do you get an attached picture to display in HTML format so that it can be viewed in a remote email.

    Sub picture()
        Set objOutlook = CreateObject("Outlook.Application")
        Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
        Set objOutlookRecip = objOutlookMsg.Recipients.Add(!["email0@live.com"])
        objOutlookRecip.Type = olTo
        objOutlookMsg.Subject = "Picture"
        objOutlookMsg.BodyFormat = olFormatHTML
        objOutlookMsg.HTMLBody = "<HTML><head></head><BODY><img src=C:\path\picture.jpg /></BODY></HTML>"
        objOutlookMsg.Send
    End Sub


    Friday, March 29, 2013 3:44 PM

Answers

All replies

  • You need to use an email address when you add a recipient to an email. I'm not sure what language you're using but in VBScript you'd use
     
    Set objOutlookRecip = objOutlookMsg.Recipients.Add("email0@live.com")
    It's always a good idea also to call the Resolve() method on any recipient you add and to test the result.
     
    Typically you'd attach a picture to the email and then reference the picture attachment something like this:
     
        src="cid:image001.jpg@01CE2C73.9BD96560"
     
    Where the ContentID property of the image attachment was "01CE2C73.9BD96560".
     
    An alternative, although most Outlook installations won't download images from the Internet without user intervention, is to reference a file path on a Web site. A file reference to your computer won't work for anyone but you.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "Philosophiae" <=?utf-8?B?UGhpbG9zb3BoaWFl?=> wrote in message news:717c7f39-379f-45aa-817e-f71db81e4244...

    I have two questions.  One how do you get the Recipient email to work.  Two how do you get an attached picture to display in HTML format so that it can be viewed in a remote email.

    Sub picture()
        Set objOutlook = CreateObject("Outlook.Application")
        Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
        Set objOutlookRecip = objOutlookMsg.Recipients.Add(!["email0@live.com"])
        objOutlookRecip.Type = olTo
        objOutlookMsg.Subject = "Picture"
        objOutlookMsg.BodyFormat = olFormatHTML
        objOutlookMsg.HTMLBody = "<HTML><head></head><BODY><img src=C:\path\picture.jpg /></BODY></HTML>"
        objOutlookMsg.Send
    End Sub



    Ken Slovak MVP - Outlook
    Friday, March 29, 2013 3:53 PM
    Moderator
  • This did not display the html image, img.  Is this able to display a workable html email?

    Sub picture()
        Set objOutlook = CreateObject("Outlook.Application")
        Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
        Set objOutlookRecip = objOutlookMsg.Recipients.Add("email0@live.com")
        objOutlookRecip.Type = olTo
        objOutlookMsg.Subject = "Picture"
        ' add "C:\picture.png as attachment to Outlook message
          Set colAttach = objOutlookMsg.Attachments
          Set l_Attach = colAttach.Add("C:\Users\Philosophaie\Documents\a\BALLOONS.jpg")
          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 "{0820060000000001C100000000000046}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
    End Sub
    
                            



    Friday, March 29, 2013 4:12 PM
  • You are using multiple dot notation (objOutlookMsg.Recipients.Add) so you are lweft with an implicit variable that you can release.

    Create the message using CDO 1.21 to begin with instead of reopening it with CDO later.

    Also note that you can set oSession.MAPIOBJECT to Namespace.MAPIOBJECT instead if calling oSession.Logon.


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

    Friday, March 29, 2013 4:35 PM
  • I can not get the image to appear in the html img.

    Is there a solution for this?

    And what is the exact syntax for the 

    Set Namespace.MAPIOBJECT=CreateObject("MAPI.Session")
    
    Namespace.MAPIOBJECT "", "", False, False



    Friday, March 29, 2013 4:44 PM
  • the syntax is

    oSession.MAPIOBJECT = objOutlook.Session.MAPIOBJECT


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

    Friday, March 29, 2013 4:48 PM
  • Ultimately the item must be opened and processed as an Outlook.MailItem though, as CDO 1.21 doesn't know a thing about HTML emails.
     
    Also, for the most part CDO is deprecated for use with Office. I'd probably use PropertyAccessor to set the necessary properties and do everything in the Outlook object model.
     

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "Dmitry Streblechenko _MVP_" <=?utf-8?B?RG1pdHJ5IFN0cmVibGVjaGVua28gX01WUF8=?=> wrote in message news:37a6139b-52cd-4a99-8085-9c9c540b78d7...

    the syntax is

    oSession.MAPIOBJECT = objOutlook.Session.MAPIOBJECT


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


    Ken Slovak MVP - Outlook
    Friday, March 29, 2013 5:17 PM
    Moderator
  • Is this how it looks:

           Set oSession = CreateObject("MAPI.Session")
           oSession.MAPIOBJECT = objOutlook.Session.MAPIOBJECT
            

    Should there be some numbers after the "cid:mypictures"  from the oField and oMsg variables:

            

            objOutlookMsg.BodyFormat = olFormatHTML
    
            objOutlookMsg.HTMLBody = "<HTML><head></head><BODY><center><font color=red><h1>Birthday</h1></font></center><br /><center><img src=cid:mypicture height=500 width=500/></center></BODY></HTML>"
            objOutlookMsg.Send

    All I get is the frame of 500x500.




    Friday, March 29, 2013 9:25 PM
  • Did you correctly set the PR_ATTACH_CONTENTID property? Can you see it in OutlookSpy if you select the message, click IMessage button, go to the GetAttachmentTable tab and double click on the attachment?

    Also, as Ken mentioned, there is absolutely no reason to use CDO 1.21 for this - you can do everything using the Outlook Object Model alone.


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

    Saturday, March 30, 2013 10:23 PM