Wednesday, 14 July 2010

Embed an image to an email sent via asp


I found the trick I am about to explain on the web.  When the sales manager told me to create a procedure to send newsletter through the company Intranet web site, there was really no problem (I believe almost everyone knows how to do that!). It really got challenging when he told me they wanted to send images attached to the message body. I knew how to attach documents, but embedded images? Hmmm... that was tricky. Then I found the solution.

Let's start with the code:
Dim objCDO
Set objCDO = Server.CreateObject("CDO.Message")
  objCDO.From = varsender 'Senders Email Address
  objCDO.To = varrecipient 'Recipient Email Address
  objCDO.CC = varcc 'Carbon Copy Address
  objCDO.BCC = varccn 'Carbon Copy Address
  objCDO.Subject = varsubject 'Email Subject
    varimage = "<html>"& vartext &"<br><br><img src=""cid:" & Request("varbackground") & """></html>"
    if Request("varbackground") <> "" then
     objCDO.HTMLBody = varimage 'Email Message
' Here's the good part, thanks to some little-known members.
' This is a BodyPart object, which represents a new part of the     multipart MIME-formatted message.
' Note you can provide an image of ANY name as the source, and the second parameter essentially
' renames it to anything you want. Great for giving sensible names to dynamically-generated images.
     Set objBP = objCDO.AddRelatedBodyPart(Server.MapPath(varbackground), varbackground, CdoReferenceTypeName)
'Set objBP = objCDO.AddRelatedBodyPart(Server.MapPath("/images/myimage.gif"), "myimage.gif", CdoReferenceTypeName)
' Now assign a MIME Content ID to the image body part.
' This is the key that was so hard to find, which makes it
' work in mail readers like Yahoo webmail & others that don't
' recognise the default way Microsoft adds it's part id's,
' leading to "broken" images in those readers. Note the
' < and > surrounding the arbitrary id string. This is what
' lets you have SRC="cid:myimage.gif" in the IMG tag.
     objBP.Fields.Item("urn:schemas:mailheader:Content-ID") = "<"& varbackground &">"
     objCDO.HTMLBody =vartext
   end if
objCDO.Send() 'send mail
Set objCDO = Nothing 'Clean up your objects!!!

The comments are the original ones and the forum where I found this little gem is here:

What I did is just to fit the code to my needs. If you see I added an If.. Then... Else statement just to verify the presence of an image to embed, because our people needs to send newsletter with or without embedded images. The other trick was to create a procedure to upload images to be embedded, and an interactive form where you select the image to send (among all the other usual fields). But again that's another story.


  1. Great job indeed. Thanks to page I am now one step ahead. Ir Works great. But I have a strange problem. When I added embeded image, the content of textbody properties (text portion) is lost. I see only the image I have embeded. I want to see the image at the end of the mail as a signature image. Any comment please?


    1. vartext is where the text part of the email stays. Careful! It's HTML.

    2. Thanks Marco,

      I'm geting error which says "undefined sub/function [Request])". What is the REQUEST function?

    3. You should build a form. The Request is used to gather the info posted by the form.

  2. Yes, I have modified the code to my need and it work fine. I am using VB6 and if you want I can post my code here for a reference who may need.

    Just a little small problem I have now as I don't know HTML coding.

    I have a textbox in my form to add text for body text. When I write some information more then one line in that textbox, I receive the mail in one row like this.

    The textbox content like this
    Dear Sir,
    Please find enclosed offer.

    Best regards,

    The amil I receive is;
    Dear Sir,Please find enclosed offer.Best regards.

    Your kind help will make my day.

    1. Because it's HTML. You need a line break:

    2. a br tag < br > (without spaces)

  3. I have built a code for converting the linebreaks in a textbox to HTML. And now, everything seems ok. Thanks for your kind support Marco.

    Best regards,

    Here is the code:

    Public Function TexttoHTML(Bilgi As String) As String
    k = 1
    k = InStr(k, Bilgi, Chr(13) & Chr(10))
    Do While k > 0
    Bilgi = Left(Bilgi, k - 1) & "
    " & Right(Bilgi, Len(Bilgi) - k - 1)
    k = InStr(k + 2, Bilgi, Chr(13) & Chr(10))
    TexttoHTML = Bilgi
    End Function

    1. I've just noticed that, in the above code at the and of line 5, the tag
      is not showing. I think your page assumed it as a linebreak and so splited the line. :)

    2. Hello Marco,

      Have ever tried to use CC and Bcc fields? Because it seems not working. I have put valid email adress in both fields and didn't work. I am getting no error but the result is not as expected.


    3. I have tried .bcc=" instead of .bcc="" and worked. Do you have any idea of this notation in VB?

  4. I have tried .bcc="< >" instead of .bcc="" and worked. Do you have any idea of this notation in VB?

  5. ' means user comment in VB like // in C language. The right of the ' sign is assumed as comments by the compiler. "< > works fine now. I accepted it as a compiler rule and happy. :)) Thanks again

  6. Hi Marco,

    It's time to teach us how to receive mail with CDO (subject, body, attacments, images etc), isn't it? Hope you won't forsake us. :)


    1. Good try Hakan! I'm sure you know you can't receive emails with CDO...

  7. Ok then, let me change my question. It's time to teach us the way of coding to receive emails, isn't it?

    Take care

  8. How can we add "request read receipt" from the receiver while sending email with CDO?



Comments are moderated. I apologize if I don't publish comments immediately.

However, I do answer to all the comments.