Sending Emails Using CDO in WSH - Sending HTML messages
(Page 4 of 4 )
Okay, so sending text emails isn’t enough for you? This is the age of modern email after all and in most cases you’ll want to send email with clickable links and embedded images. In this case, you’ll want to send HTML formatted emails.
CDOSYS says “no problemo!” It can handle those as well. You’ll have two choices. You can either send an HTML formatted text string or you can tell CDOSYS to send an HTML document as your email body. The document can be either local or remote.
We’ll start with the first and send an HTML formatted text string. You’re going to use the same email script we’ve just created. This time you’re going to leave out the Textbody property and replace it with the appropriate HTML property or method.
object.HTMLbody
object.CreateMHTMLbody path [, flags [, username, password]]
The HTMLbody property returns or sets the HTML body part of your message. The CreateMHTMLbody method will create an email message from the HTML document you specify. The path may be remote or local but must be a full local path or a fully qualified URI. The flags attribute accepts a constant value that tells the CDOSYS object what parts, if any, should be omitted from the message. Finally, you may supply a username and password if the HTML document you specify is password protected.
Fully qualified URIs must begin with a valid protocol. Here are common examples:
- http://www.mysite.com/index.html
- file:///C:/pages/index.htm
So if we wanted to send our local HTML page, we would change our script by replacing the first section of code with the section below. All I’ve done is removed the line setting the Textbody property and replaced it with a line that sets the HTMLbody property instead. Of course, I’ve formatted the text in HTML as well.
Set objMessage = CreateObject("CDO.Message")
' Set Email Headers
objMessage.From = "sender@mymail.com"
objMessage.To = "recipient@mail.com"
objMessage.CC = "copyto@mail.com"
objMessage.BCC = "blindcopy@mail.com"
objMessage.ReplyTo = "replyto@mail.com"
objMessage.Subject = "Test email with CDOSYS!"
' Construct Email Body
objMessage.HTMLbody = "<h1>This is a test email using CDOSYS.</h1>"
To send a local or remote HTML document, we would replace the same line with a call to the CreateMHTMLbody method instead. Here’s an example of what that code might look like.
Set objMessage = CreateObject("CDO.Message")
' Set Email Headers
objMessage.From = "sender@mymail.com"
objMessage.To = "recipient@mail.com"
objMessage.CC = "copyto@mail.com"
objMessage.BCC = "blindcopy@mail.com"
objMessage.ReplyTo = "replyto@mail.com"
objMessage.Subject = "Test email with CDOSYS!"
' Construct Email Body
objMessage.CreateMHTMLbody "C:pagesindex.htm"
The rest of your script would remain exactly the same. However, sending HTML messages does raise one problem. Some people are still using email clients that do not support HTML messages.
In those cases your may want to send what’s known as a multi-part message that includes both an HTML and a plain text portion. In this case, the HTML is displayed by default, and the plain text is displayed whenever HTML is not supported.
CDOSYS makes sending multi-format messages very easy. Just add the following line and let the AutoGenerateTextBody property do the rest.
objMessage.AutoGenerateTextBody = True
The AutoGenerateTextBody property tells CDOSYS whether or not to auto-generate a multi-format message by creating a text portion from the HTML part that you have provided. Its default is False.
You now have everything you need to send email messages in WSH. Keep in mind that the CDO objects do provide much more functionality than I have been able to include in this article. You can request Deliver Service Notifications and Read Receipts, check local email boxes, access local Address Books, check and post to newsgroups, and more. Unfortunately, I don’t have enough room to cover it all, so I’m forced to wrap this up. Until next time, keep coding!
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |