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:
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.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.