Main Contents

Embed an image in CDO e-mail

October 29, 2008

I had the need to generate nicely formatted e-mails from a website (IIS, ASP), which meant using images. Often times linked images are blocked from view by default in mail clients. Embedded images in HTML e-mail are more difficult to block within the client, but may be blocked at the SPAM/Filtering gateway level, especially if there are more than one embedded images, or if there is no other content except images. If you want more reliable delivery, use linked images. As a courtesy, it is common to provide a link at the top of the e-mail that says something like “If you can’t see the images in this e-mail, click here” which will load the same content from your web server in a browser. It can become tedious to manage, though. If you want better presentation, but risk less than 100% delivery, use embedded images.

Here’s a simple example of how to embed an image using CDO from ASP vbscript.

<%
Dim CDO, RBP
Set CDO = Server.CreateObject("CDO.Message")
CDO.MimeFormatted = True
CDO.To = "test@test.com"
CDO.From = """Test User"" test@test.com"
CDO.Subject = "E-mail with embedded image"
CDO.HTMLBody = "<html>Check this out: <img src=""cid:test.gif""></html>"
Set RBP = CDO.AddRelatedBodyPart(Server.MapPath("/images/test.gif"), "test.gif", 1)
RBP.Fields.Item("urn:schemas:mailheader:Content-ID") = "<test.gif>"
RBP.Fields.Update
CDO.Send
%>

And the same example with a link. Make sure the image exists at the correct url.

<%
Dim CDO
Set CDO = Server.CreateObject("CDO.Message")
CDO.MimeFormatted = True
CDO.To = "test@test.com"
CDO.From = """Test User"" test@test.com"
CDO.Subject = "E-mail with embedded image"
CDO.HTMLBody = "<html>Check this out: <img src=""http://www.yourwebsite.com/images/test.gif""></html>"
CDO.Send
%>

Filed under: Microsoft |

2 Comments

  1. LEopoldo November 26, 2008 @ 9:41 am

    Who can i do this for C#?

    this line i canĀ“t translate for c#

    Set RBP = CDO.AddRelatedBodyPart(Server.MapPath(”/images/test.gif”), “test.gif”, 1)

    help me please!

  2. Vaxeline February 5, 2009 @ 3:40 am

    But how do I do it if I don’t want to embed the image in mail? I just want the image to be downloaded from my site when the user opens the email. I cannot find anywhere the answer for this. Thanks.