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 = "[email protected]"
objEmail.To = "[email protected]"
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

Don Jones is a multiple-year recipient of Microsoft’s MVP Award, and is Curriculum Director for IT Pro Content for video training company Pluralsight. Don is also a co-founder and President of PowerShell.org, a community dedicated to Microsoft’s Windows PowerShell technology. Don has more than two decades of experience in the IT industry, and specializes in the Microsoft business technology platform. He’s the author of more than 50 technology books, an accomplished IT journalist, and a sought-after speaker and instructor at conferences worldwide. Reach Don on Twitter at @concentratedDon, or on Facebook at Facebook.com/ConcentratedDon.

Featured

  • Windows 10 Preview Adds Ability To Display Linux Distro Files

    Microsoft on Wednesday announced Windows 10 preview build 19603, which adds easier access to installed Linux distro files using Windows File Explorer.

  • Microsoft 365 Business To Get Azure Active Directory Premium P1 Perks

    Subscribers to Microsoft 365 Business (which is being renamed this month to "Microsoft 365 Business Premium") will be getting Azure Active Directory Premium P1 licensing at no additional cost.

  • How To Use .CSV Files with PowerShell, Part 1

    When it comes to bulk administration, few things are handier than .CSV files. In this two-part series, Brien demos his top techniques for working with .CSV files in PowerShell. First up: How to create a .CSV file.

  • SameSite Cookie Changes Rolled Back Until Summer

    The Chromium Project announced on Friday that it's delaying enforcement of SameSite cookie changes, and is temporarily rolling back those changes, because of the COVID-19 turmoil.

comments powered by Disqus

Office 365 Watch

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.