Sending Emails Using CDO in WSH - Configuring the SMTP server
(Page 3 of 4 )
In order to send the message, CDO needs to know some information about the SMTP server you’ll be using to send it. While this can be a local server, in most cases it’s probably going to be remote.
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = _
"smtp.mymail.com"
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
The Configuration.Fields Property returns a collection of settings used to configure your email message (or newsgroup post as CDO also supports NNTP) for SMTP. There is a long list of configurations available. I’m only showing you the ones you’ll need most.
For more information I suggest checking out Chapter 6 of Brian Knittel’s book “Windows XP Under the Hood,” a fantastic Windows Scripting book available from Que. The chapter, titled “Messaging Objects,” takes an in-depth look at the CDO object. You can pick it up at both Barnes&Noble and Amazon.com.
The sendusing property will almost always be set to 2. This tells CDO to send your message directly to the SMTP server that you provide. A value of 1 would indicate to drop the message into the IIS SMTP service pickup directory instead.
The smtpserver item accepts either a valid hostname or IP address for the SMTP server. The smtpserverport item should be set to whatever port your SMTP server operates on. In most cases this is port 25.
If your SMTP server requires authentication, you’ll need to add a few more configuration fields.
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusername") = _
"user"
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendpassword") = _
"password"
The smtpauthenticate item accepts one of three cdoProtocolsAuthentication values. Use its default 0 for no authentication, 1 for basic (clear-text) authentication, or 2 to use the credentials of the currently logged on user.
If your outgoing mail server requires a secure connection, you’ll also need to set the smtpusessl item. It accepts a Boolean value and defaults to false.
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
After setting all of the configuration items, you’ll need to make a call to the Update method in order to save those settings in the Fields collection.
objEmail.Configuration.Fields.Update
objEmail.Send
Finally, you can make a call to the CDO object’s Send method and send your message zipping off through cyberspace. One nice thing about using CDOSYS instead of CDONTS is that you can use this as many times as necessary. CDONTS only supports sending one message at a time and requires you to recycle the message object each time you need it.
Next: Sending HTML messages >>
More Windows Scripting Articles
More By Nilpo/Developer Shed Staff Writer