A Small Script (Send Email via CDO)

Originally published November 30, 2004

 

Several months ago I wrote a small script about sending email via the Outlook object model. You can see that article here.

The Outlook object model has a couple of problems - it requires user approval, and you don't have access to all the fields you may want to use. It has a major advantage - it uses the Outlook engine and is very powerful for examining Outlook objects.

There are alternatives. CDO (Collaboration Data Objects) is one. CDO has a major feature - it doesn't require user approval. It has a major problem - it requires an SMTP server or that it's running on a computer where SMTP is installed.

However, you probably know the name of your Exchange server. So, you can easily put that into the script below.

The major reason I needed this was to be able to specify the “From“ address. It ended up having other benefits for me.Just modify the obvious strings, and it'll work for you. The strSMTPServer can be either a NetBIOS (short) name or a FQDN (fully qualified domain name).

Option Explicit

Const strSMTPServer = "exchange-server"
Const strFrom       = "mbs@zippy-do-dah.com"
Const strTo         = "dest-addr@gmail.com"

Dim objMail

Set objMail = CreateObject ("CDO.Message")

objMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing")      = 2
objMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver")     = strSMTPServer
objMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objMail.Configuration.Fields.Update

objMail.From     = strFrom
objMail.To       = strTo

objMail.Subject  = "Some silly subject"
objMail.Textbody = "This is a" & vbCRLF & "text body."

objMail.Send

Published Tuesday, November 13, 2007 7:18 PM by michael
Filed under: ,

Comments

Monday, October 06, 2008 1:04 PM by Michael's meanderings...

# Script for From The Field

The authors of the upcoming "Windows Server 2008 Terminal Services Resource Kit" asked me if