Sending Emails Using CDO in WSH - Preparing your message
(Page 2 of 4 )
We’ll begin by setting our basic email fields: To, From, CC, BCC, etc. These are the fields used to construct the email’s header.
Properties
object.To
object.From
object.CC
object.BCC
object.ReplyTo
object.Subject
object.Textbody
object.HTMLBody
object.Configuration.Fields.Item(schema)
Methods
object.Configuration.Fields.Update
object.CreateMHTMLBody htmlbody
object.AddAttachment file
object.Send
All of the properties listed above accept text strings and are used to set various email fields. I’ll go into details about them as we use them. These are much easier to learn when you see them in action.
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.Textbody = "This is a test email using CDOSYS."
Here you can see I’ve set the standard email fields. Only the From, To, and Textbody fields are required. The ReplyTo field is the address that replies should be sent to if you don’t want them going to the From email address by default.
All of the email address fields may use any standard email address format:
- "Full Name" <email@domain.com>
- "Full Name" email@domain.com
- Full Name <email@domain.com>
- email@domain.com
You may add multiple recipients to any of the address fields by separating multiple addresses with commas. If you use any of the formats involving quotation marks, you will need to escape them in your script.
The Textbody property accepts a single text string as a value. This can have any number of carriage returns as needed. The text string can be read from an existing text file, read from any TextStreamObject (such as a program’s output) or coded manually.
object.AddAttachment localpath | remoteurl[, username, password]
Want to add some attachments? That’s no problem either.
objMessage.AddAttachment "C:attachment.txt"
AddAttachment can accept either a local file path or a valid URL. If the URL is password protected, be sure to add the username and password as well. You may call AddAttachments as many times as needed to add multiple attachments to your message.
Next: Configuring the SMTP server >>
More Windows Scripting Articles
More By Nilpo/Developer Shed Staff Writer