add

Tuesday, July 21, 2009

How to add image to mail body using CDO

Many times we face situation like, we need to send image in the html body of the mail that too using CDO.

Suppose we have an image at local server from where MailSend Code will get fire, here is the code:

Add this line of code to your CDO code to send image as a mail body:


Set objMessage = CreateObject("CDO.Message")
Set objBP = objMessage.AddRelatedBodyPart("C:\Users\Varun.Sharma\Shell\1.jpg", "1.jpg", CdoReferenceTypeName)
objBP.Fields.Item("urn:schemas:mailheader:Content-ID") = "<1.jpg>"
objBP.Fields.Update

Here, in AddRelatedBodyPart,
argument 1 is : physical location of image
argument 2 is : identifier of the image (any name)
argument 3 is : To tell CDO to send the image as well

In next line, just put image identifer to the right hand side.

When sending mail refer the image in html body like this:

mail.HtmlBody="<img src=1.jpg" >

For full code of sending mail using CDO, check the following link:
How to send mail using cdo in vbscript

No comments: