Mr. Roboto

In the Mail

Want to send an e-mail from the command line? Give Blat a try.

I spend a lot of time in newsgroups and discussion forums. A common question involves sending e-mail from the command line. The answer? There are technologies you can implement in VBScript to accomplish this.

If you're using PowerShell, there are several third-party cmdlets that will send an SMTP message from the command line. But what if you don't script or aren't ready for PowerShell? Then your answer is a free, public-domain utility called Blat. I know the name leaves something to be desired, but it gets the job done with minimum effort -- and it's free. With Blat, you can send an e-mail via the command prompt with practically no effort.

Blat is a Win32 console application that has been around for a long time and is now in the public domain. It will send messages via an SMTP server. Blat isn't an SMTP server itself -- it's a command-line SMTP client that supports all the features you're likely to need, including CC, BCC, impersonation and attachments.

Give Blat a Try
To get started, go to blat.net and download the installation file. Extract the files to any directory of your choosing. You won't really need them, but you might want the documents for reference. I copy the blat.exe to my System32 directory so I can call it from any command prompt. Open a command prompt and type blat -help to see the complete syntax. The basic message syntax is:

C:\> Blat -to jhicks@sapien.com -f 
mrroboto@redmondmag.com 
-subject "Check this out" 
-body "Let's get blatted!" 
-server mail.redmondmag.com

If you need to specify authentication, add something like this to your command:

-u mrroboto@redmondmag.com 
-pw $ecr3+

If you're going to use the same server settings repeatedly, then Blat can store them in the registry:

C:\> blat -savesettings mail.red 
mondmag.com mrroboto@red 
mondmag.com - 25 - 
mrroboto@redmondmag.com 
$ecr3+

This command will create a default profile using mail.redmondmag.com on port 25 with the specified credentials in the registry under HKLM\Software\ PublicDomain\Blat. But don't panic; the user name and password are stored as encrypted strings.

Now, let's send some mail. If you have a text file that you want to use as the message body, use syntax like this:

C:\> blat c:\files\message.txt -to 
admin@mycompany.com -cc 
mgr@mycompany.com -subject 
"Network Status"

Blat will use the default registry settings for your SMTP connection. If you always send messages to the same people, you can create text files and reference them using the -tf and -cf parameters.

Real-World Use
Now that you can manually send mail from the command line, let's apply it to a practical solution. I'm betting you have a few services on critical servers, and you'd like to know when they stop. Here's my solution using Blat. You'll need to download the notification script from jdhitsolutions.com/scripts. The batch file sends a message using your default Blat profile. You need to specify a service name and a send-to address. The script will send a canned message and then attempt to restart the failed service.

To set this up, open the services-management console and go to the service you want to monitor. Go to the Recover tab and click First Failure, then choose Run a Program. In the Program field, type the full filename and path to notify.bat. In the Command Line Parameters section, enter the service name and an e-mail address for the SendTo parameter. The service name must be something you'd use if you were using the SC.EXE or NET START. Don't use commas to separate the parameters.

You can test this by killing the process associated with the service. Don't merely stop use of the service; you must force it to terminate abnormally for the recovery task to execute. The script should restart the service and use Blat to send an e-mail notification.

This example is merely one situation in which a command-line tool like Blat can be useful. I'm sure you'll come up with your own, and if you do, I hope you'll let me know.

About the Author

Jeffery Hicks is a multi-year Microsoft MVP in Windows PowerShell, Microsoft Certified Professional and an IT veteran with almost 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He works today as an independent author, trainer and consultant.

comments powered by Disqus

Reader Comments:

Wed, Dec 31, 2008 Bjorn Watland St Paul, MN

Blat is a great app for those admins who either are still developing BAT files or need to maintain these scripts. I use Blat to provide error reporting within scripts, so not only do I get a email if a script ran or not, but I can get details on where in the script everything fell apart.

Fri, Nov 7, 2008 Anonymous Anonymous

The paragraph on storing the registry settings saved me a lot of headaches - 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.