SolutionBase: Send mail messages with VBScript

By mastering VBScript, you can automate many mundane network administration duties. Here's how you can use a VBScript to automatically send e-mail.

This script is one of the handiest and most often-used tools in my toolbox. I often find myself in one of the following situations:
  • I have a backup or other scheduled job that I want to view the log files for, and I want to have them automatically emailed to me every day.
  • I have a mail server that I need to test whether mail relaying is working (or not) as expected.
  • I want to send an email or file to someone quickly, from a server, without installing or configuring an email client.
One way to accomplish these tasks is to telnet to port 25 of your email server and run a batch of SMTP commands. However, unless you do this every day, the commands are difficult to remember, and it takes a while to type in all of the commands. Using this script bypasses all of this.

Running the script

Running the script is very simple. First, download it and extract the SendMsg.txt file. You'll then need to rename the file to SendMsg.vbs. We don't automatically save the file with that extension because many virus scanners will interpret a VBS inside of a ZIP file as being a virus. It's not. You're perfectly safe. Rename the file and you're ready to go.

To run the script interactively, simply double-click on SendMsg.vbs in Windows Explorer, or type cscript SendMsg.vbs (without the quotes) from a command prompt. Then simply follow the prompts.

If you want to run the script in a non-interactive mode (a scheduled job, for instance), a couple of slight modifications in the script will be necessary. Every line in the script that contains "InputBox" must be changed; eliminate the InputBox, and replace the entire function with just the string you need.

For example, lines 25 & 26:

objEmail.From = InputBox("Please type the SMTP address of the sender." & vbCrlf & _

ï¿?ï¿?ï¿?ï¿?ï¿? "(Example:", "FROM:")

would simply become:

objEmail.From = ""

Also, lines 36-42 would be completely eliminated, with the exception of line 40. This line (if attachments are needed) would simply become:

objEmail.AddAttachment "<path>\filename.ext"

Additional resources

Be sure to check out other TechRepublic scripting articles:

If you want to get started with scripting, or would like access to an extensive repository of scripts, be sure to check out the Microsoft TechNet Script Center. If you need a scripting editor, check out VBSEditor by Koan Software (free), or PrimalScript by Sapien.