antic
Perch
You know that Eureka moment, when you've been trying to work out how to achieve something, and all you find are crumbs on the web, but eventually you try a few things, nut it out and find a solution that suddenly works?
Well, if anyone has ever wanted to *embed* images in their HTML emails (i.e. as opposed to using IMG tags linked to external images, which sometimes don't show in browsers like Thunderbird due to security settings) then read on.
I'll be brief, because it's 3am here, as it always is when these things happen. So here's the code:
That's how you embed images in HTML emails using CDO in ASP. The equivalent will work in ASP.NET of course. Hope that helps someone wanting to do the same thing.
Well, if anyone has ever wanted to *embed* images in their HTML emails (i.e. as opposed to using IMG tags linked to external images, which sometimes don't show in browsers like Thunderbird due to security settings) then read on.
I'll be brief, because it's 3am here, as it always is when these things happen. So here's the code:
Code:
Const CdoReferenceTypeName = 1
Dim objCDO, objBP
Set objCDO = Server.CreateObject("CDO.Message")
objCDO.MimeFormatted = True
objCDO.To = "[email protected]"
objCDO.From = "[email protected]"
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