Script Tips

Mail at the Ready

Quick way to provide e-mail on your systems via scripting.

I’m often asked how to send a quick e-mail from within a script. There are lots of ways, especially if you have some handy, third-party, e-mail-sending components on your system. Fortunately, the Scripting Guys at Microsoft have a simpler way, using nothing but the native capabilities of any recent version of Windows (e.g., Win2K or newer). You’ll find their solution by clicking here and it looks something like this:

Set objEmail = CreateObject("CDO.Message")

objEmail.From = "me@company.com"
objEmail.To = "you@company.com"
objEmail.Subject = "Script Message"
objEmail.Textbody = "This is a message for you."
objEmail.Configuration.Fields.Item _
  ("http://schemas.microsoft.com/cdo/configuration/" &_
   "sendusing") = 2
objEmail.Configuration.Fields.Item _
  ("http://schemas.microsoft.com/cdo/configuration/" &_
   "smtpserver") = "smtp.company.com"
objEmail.Configuration.Fields.Item _
  ("http://schemas.microsoft.com/cdo/configuration/" &_    "smtpserverport") = 25
objEmail.Configuration.Fields.Update
objEmail.Send

The big updates to make are the From, To, Subject, and Textbody fields, and the name of your SMTP server (which can be an IP address, too). This utilizes Collaborative Data Objects (CDO), which is a wrapper around the Messaging Application Programming Interface (MAPI). On some systems, Outlook may intercept this script and display a warning dialog box; that shouldn’t happen if Outlook isn’t installed.

Possible uses? Scripts that run under Task Scheduler and do some kind of inventory, then e-mail you the results. Or scripts that send e-mail alerts. Or scripts that just spam everybody in the organization with an invitation to...no, wait, bad idea. Stick with alerts and reports.

About the Author

With more than fifteen years of IT experience, Don Jones is one of the world’s leading experts on the Microsoft business technology platform. He’s the author of more than 35 books, including Windows PowerShell: TFM, Windows Administrator’s Scripting Toolkit, VBScript WMI and ADSI Unleashed, PHP-Nuke Garage, Special Edition Using Commerce Server 2002, Definitive Guide to SQL Server Performance Optimization, and many more. Don is a top-rated and in-demand speaker and serves on the advisory board for TechMentor. He is an accomplished IT journalist with features and monthly columns in Microsoft TechNet Magazine, Redmond Magazine, and on Web sites such as TechTarget and MCPMag.com. Don is also a multiple-year recipient of Microsoft’s prestigious Most Valuable Professional (MVP) Award, and is the Editor-in-Chief for Realtime Publishers.

comments powered by Disqus

Reader Comments:

Tue, Jun 21, 2005 straightshooter Anonymous

Awhile back, I did something similar as a "test" of our users opening unknown, "virus" laden mail. I sent it to everyone at different times during the day from a spoofed address and attached a simple bat file that simulated a nasty virus. It was a good learning tool for them!

Tue, Jun 21, 2005 Joey Anonymous

Can someone list the object for setting the importantance on the email (ie low, med, high). thanks.

Add Your Comment Now:

Your Name:(optional)
Your Email:(optional)
Your Location:(optional)
Comment:
Please type the letters/numbers you see above

Redmond Tech Watch

Sign up for our newsletter.

I agree to this site's Privacy Policy.