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

4 comments:

Marcellius Tiongco said...

How would I set up an auto thank you message for the person who fills out the form?

Varun Sharma said...

Hi There,

If you have a form, you must be having some code to store the form contects to some database .

In that code only, you can just send the thankyou mail to the user's email after storing the information.

Let me know whether this matches your scenario or not?

Albert Andrada said...

Really Nice Information,Thank You Very Much For Sharing.
Web Development Company

Anonymous said...

Thanks a lot, Awesome tip. This saved my day!!!

Thanks,
Ko