Embed Image In ASP Classic Email

Naryan108
Naryan108 used Ask the Experts™
on
Hi, I know this is nothing new and there's even a few similar threads out there, but I cannot seem to find a satisfactory solution for this.

I've tried this:
strBody = "<HTML><BODY><p>peace on earth</p><p><IMG src=""image.jpg""></p><p>more peace</p></BODY></HTML>"

ObjSendMail.AddRelatedBodyPart Server.MapPath("image.jpg"),"image.jpg",cdoRefTypeLocation      

and it does embed something...the image is attached and there's a little square where the image should be.

Please help!                  
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
You must have the full URL of the image, it won't embed it but it will download it from a source and display it. So it should be:

strBody = "<HTML><BODY><p>peace on earth</p><p><IMG src=""http://www.mywebsite.com/image.jpg""></p><p>more peace</p></BODY></HTML>"

Author

Commented:
thanks for the comment.  Yes, I'm aware of that way of doing which is ultimately better I think, but a client wants it embedded so it can be viewed off-line and by default most email clients do not download the image automatically (for good reason).

That's why it's gotta be embedded.
See the code and comments below...  If you need anything more let me know.
Const CdoReferenceTypeName = 1
Dim objCDO, objBP
Set objCDO = Server.CreateObject("CDO.Message")
objCDO.MimeFormatted = True
objCDO.To = "you@yourdomain.com"
objCDO.From = "you@yourdomain.com"
objCDO.Subject = "Embedded image demo"
objCDO.HTMLBody = "<html>Check this out: <img src=""cid:myimage.gif""></html>"

' 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("/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") = "<myimage.gif>"
objBP.Fields.Update

objCDO.Send

Open in new window

Author

Commented:
Thank you - that's perfect!  I can't say I understand it completely, but it works.  I'll do my homework...

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial