none
Automating Outlook via VB .NET to send html e-mails with inline images RRS feed

  • Question

  • A while back I wrote some VB.Net code to automate Outlook to send an e-mail:

    Dim oApp As Object
    Dim oMsg As Object
    Dim nBodyLen As Integer

    oApp = CreateObject("Outlook.Application")
    oMsg = oApp.CreateItem(0)
    oMsg.To = "test@abc.com"
    oMsg.Subject = "Test"
    oMsg.HTMLBody = htmlBodyText
    oMsg.Send()

    (I'm using late-binding because I want this to work with any version of Outlook)

    This works fine as long as the HTML (htmlBodyText in code above) doesn't contain any images. Unfortunately some of the HTML messages the users of my app are sending have links to images, for example:

    <img width=1024 height=768 id="_x0000_i1025" src="file:///C:\Users\Public\Pictures\Sample%20Pictures\Desert.jpg">

    This obviously won't work because when the recepient receives this e-mail, he will neither have the actual image, nor even the directory structure, as specified in the 'src' attribute of the image.

    So I have two problems to overcome, how do I send the image together with the e-mail message, and secondly, how do I reference that image in the src tag of the image in the HTML body.

    I know there is the option to upload the images to a publicly accessible online host, and then reference the images in the src tag of the images, but I don't want to go that route.

    I performed a quick test to see how Outlook handles this. I created a new e-mail, and attached an image to it, and sent it to myself. When I received the e-mail, I inspected the HTML source code. I see that it had the following markup in the src tag of the image:

    <img width=1024 height=768 id="Picture_x0020_1" src="cid:image001.jpg@01CD230C.CC6F2C30">

    How does that work? I'm assuming Outlook is sending the image together with the HTML body to the recepient's e-mail server, and that the content-id above is pointing to that image on the e-mail server, so that the recepient's e-mail client can then download the image?

    Alternatively I can perhaps include the images as attachments to the e-mail? And then reference the attachments in the src tags of the images? But I have no idea how to do that either.

    In summary, if anyone could point me in the right direction to being able to send an HTML e-mail with inline images, via Outlook, from my VB .NET or C# application, I would greatly appreciate ad it. (Preferably using late-binding as in my sample code above, so that it works with any version of Outlook)?

    Thanking you in advance...

    Fabricio
    Pretoria, South Africa


    Fabricio Rodriguez Pretoria, South Africa


    Wednesday, April 25, 2012 4:40 PM

Answers

  • Yes, you will need to add images as attachments and make sure their PR_ATTACH_CONTENT_ID property (you can set it using Attachment.PropertyAccessor) matches the value of the cid attribute of the image.

    Have a look at an existing message with OutlookSpy (click IMessage button and look at the GetAttachmentTable tag) .


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

    Friday, April 27, 2012 6:06 AM
  • or you could post images on http server and use links to that files (of course outlook will display bar with question about downloading those files)
    Friday, April 27, 2012 6:40 AM

All replies

  • You dont have to do that but you can send formatted html email messages using a sub class under the system class in vb.net with html content.

    Once you eliminate the impossible, whatever remains, no matter how improbable, must be the truth. - "Sherlock holmes" "speak softly and carry a big stick" - theodore roosevelt. Fear leads to anger, anger leads to hate, hate leads to suffering - Yoda. Blog - http://jefferycarlsonblog.blogspot.com/


    • Edited by The Thinker Wednesday, April 25, 2012 6:53 PM
    Wednesday, April 25, 2012 6:52 PM
  • Hi Fabs1977,

    Welcome to the MSDN forum.

    There is a special forum to deal with the Outlook issue. For better support, I will move this thread to Outlook for Developers forum.

    Please share more information about the version of outlook and any other things, it will help us to help your easier.

    Sorry for any inconvenience and have a nice day.


    Mark Liu-lxf [MSFT]
    MSDN Community Support | Feedback to us

    Friday, April 27, 2012 2:30 AM
    Moderator
  • Yes, you will need to add images as attachments and make sure their PR_ATTACH_CONTENT_ID property (you can set it using Attachment.PropertyAccessor) matches the value of the cid attribute of the image.

    Have a look at an existing message with OutlookSpy (click IMessage button and look at the GetAttachmentTable tag) .


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

    Friday, April 27, 2012 6:06 AM
  • or you could post images on http server and use links to that files (of course outlook will display bar with question about downloading those files)
    Friday, April 27, 2012 6:40 AM
  • Sorry for the late reply Dmitry - I've been in Mozambique the past few days with no internet access. Your answer pointed me in the right direction, and thanks to it I came right. Thank you for your support.

    Kind Regards

    Fabricio Rodriguez
    Pretoria, South Africa


    Fabricio Rodriguez Pretoria, South Africa

    Thursday, May 3, 2012 2:13 PM
  • Hi Fabricio - can i see your souce code. We have the same problem but I'm quite lost. thanks.
    Wednesday, November 28, 2012 8:37 AM
  • Wednesday, November 28, 2012 9:05 AM