The VBScript Network and Systems Administrator's Cafe:

sending emails

May 23 2008   2:39PM GMT

Sending e-mails with VBScript using the cdo.sys object



Posted by: Jerry Lees
sending e-mails, cdo.message, cdo.sys, cdosys object, sending emails

Writing text files, Word docs, Excel spreadsheets with VBScript is great and all– but sometimes you just need a process to run and notify you when something happens. (Without remembering to check a file every day.) In fact, sometimes it’s nice to walk in and receive an email in your inbox to remind you– or let you know all is well in the world.

 To accomplish this on windows 2003 and above the CDOSYS object is our best friend. There are a few caveats to using this script though. Note that (as I recall) CDOSYS is not available prior to Windows 2000, so if you’re running windows NT 4.0 you’ll need to use CDO.NTS instead, which is slightly different in syntax. Also, the object is installed as part of the SMTP service, so you will need the SMTP service installed on the system this script runs on to make it work. (Yes, it will work on Windows XP as well with the SMTP service installed.)

Using CDOSYS is really easy. Basically, you create the CDO.Message object and then set 4 properties (TO, SENDER, TEXTBODY, and SUBJECT) then call the SEND method to actually do the sending of emails. Literally, it’s that simple. If you don’t believe me, check out the following code:

      dim oMail
      set oMail = CreateObject(”CDO.Message”)
      oMail.To = “myemail@somedomain.com
      oMail.Sender = “
anotheremail@anotherdomain.com
      oMail.TextBody = “Your ticket has been submitted.”
      oMail.Subject = “CDO.SYS Test”
      oMail.Send        
      set oMail = nothing
      WScript.Echo “Message was sent.”        

Simple enough, right?!?! For further information about this object you can check out the following Google search.

As always, this code works perfectly. However, sometimes the formatting of the blog breaks the code if you copy and paste it into your editor. So, if you’d like to not type or troubleshoot any syntax errors due to the copy and paste problems– I’ve provided the code for download, plus example output files  from my final tests for you. You’ll find the code and other files available for download from my website’s (www.websystemsadministration.com) File Depot under the ITKE Blog Scripts category. Enjoy and happy scripting!