Tut: How to add embedded images in CDO mail

I'd advise always using linked images (img tags in the email html) instead of embedded unless you really need to embed them for some reason.


the main reason i went looking for a means to embed images, was to avoid that terrible (red x) where my images SHOULD have been in the email message... it just seems too unprofessional to have what appears to be a "broken" message (to my clients)...

having said that, im certainly open to other means of getting images into my messages... i just find it to be a much more pleasing experience (myself) when i open email from reputable sources, and find a nicely crafted message (from vendors like TigerDirect, or online services like WoW, etc.)...
thank for your code.
I is work very good
I Haven't any problem in Outlook.

but My embbede images is failed In yahoo, aol ,hotmail.
Hi to all

I check This code succesfully to OUTLOOK Reciever.

but is Yahoo or aol or Hotmail Reciever. it don't word.
and dont embbed.

do you check it to non pop3 Email Reciver?

hi group,
thanks for the wonderful code example. I was looking for this for quite a long time...but unfortunately whatever code I could come up with, did not work with all the mail clients.
I have tested the above given code example and it works flawlessly on the following email clients.
a)outlook b)pegasus mail c) lotus note d)gmail e)yahoo

I had a further requirement that I wanted to embed two images in one mail.(the example shows embedding only one)

as suggested by the orginal author, we need to create one more object of add relatedbodypart like this
Set objBP2 = objCDO.AddRelatedBodyPart(Server.MapPath("/images/myimage2.gif"), "myimage2.gif", CdoReferenceTypeName)
objBP2.Fields.Item("urn:schemas:mailheader:Content-ID") = "<myimage2.gif>"

however I tried a different method, instead of creating objBP1 and objBP2 I used the same but in between I wrote objBP.Fields.Update. Now its working for me.
my code:
Set objBP = objCDO.AddRelatedBodyPart("c:\birthday\cake4.jpg", "cake.jpg", CdoReferenceTypeName)
objBP.Fields.Item("urn:schemas:mailheader:Content-ID") = "<cake.jpg>"
Set objBP = objCDO.AddRelatedBodyPart("c:\birthday\logo.jpg", "logo.jpg", CdoReferenceTypeName)
objBP.Fields.Item("urn:schemas:mailheader:Content-ID") = "<logo.jpg>"

once again thanks for the code...it works like a charm ! ! !

Hi. Can you advise how I would change the following to support embedding an email which I can use when doing an email broadcast?

Hi adhunna, it works exactly the same as my original post, read it carefully. There are a few errors in your code so it won't work as you have it. The following code works fine for me.

dim cdoMessage, cdoConfig, sch

sch = "http://schemas.microsoft.com/cdo/configuration/"
Set cdoConfig = Server.CreateObject("CDO.Configuration")
set cdoMessage = Server.CreateObject("CDO.Message")

With cdoConfig.Fields
  .Item(sch & "sendusing") = 2 ' cdoSendUsingPort
  .Item(sch & "smtpserver") = "<your SMTP server>"
  .Item(sch & "sendusername") = "<SMTP username>"
  .Item(sch & "sendpassword") = "<SMTP password>"
End With

Set cdoMessage.Configuration = cdoConfig
cdoMessage.From = "[email protected]"
cdoMessage.ReplyTo = "[email protected]"
cdoMessage.To = "<your test email address>"
cdoMessage.Subject = "Test subject"
cdoMessage.HtmlBody = "<html><body><img src=""cid:myimage.gif""/></body></html>" & vbCrLf

Set objBP = cdoMessage.AddRelatedBodyPart("http://localhost/integral/survey/images/tpl5/head-logo.gif", "myimage.gif", CdoReferenceTypeName)
objBP.Fields.Item("urn:schemas:mailheader:Content-ID") = "<myimage.gif>"