In the Mail
Want to send an e-mail from the command line? Give Blat a try.
- By Jeffery Hicks
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 email@example.com -f
-subject "Check this out"
-body "Let's get blatted!"
If you need to specify authentication, add something like this to your command:
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 - 25 -
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
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.
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.
Jeffery Hicks is a Microsoft MVP in Windows PowerShell, Microsoft Certified Trainer and an IT veteran with over 20 years of experience, much of it spent as an IT consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He works today as an independent author, trainer and consultant. Jeff writes the popular Prof. PowerShell column for MPCMag.com and is a regular contributor to the Petri IT Knowledgebase and 4SysOps. If he isn't writing, then he's most likely recording training videos for companies like TrainSignal or hanging out in the forums at PowerShell.org. Jeff's latest books are Learn PowerShell 3 in a Month of Lunches, Learn PowerShell Toolmaking in a Month of Lunches and PowerShell in Depth: An Administrators Guide.
You can keep up with Jeff at his blog http://jdhitsolutions.com/blog, on Twitter at twitter.com/jeffhicks and on Google Plus (http:/gplus.to/JeffHicks)