I was writing a batch script to back up some critical files for a client on a periodic basis. Part of the script needed to email the client when the backup operation completed. Through a little googling, the Windows command line utility Blat popped up as the recommended solution. This article details the process I went through to get Blat working with Google SMTP (using stunnel), and to ultimately send an email via the Windows command line.
Blat is a Windows command line utility that can be downloaded here. At the time of this writing, I downloaded the 64-bit, Full Version of Blat (blat311_64.full.zip). After uncompressing the zip file, I extracted blat.exe from the depths of the container. This is all I needed to add to my working directory to get the full power of Blat at my command line fingertips.
Blat offers a plethora of different options to send an email. I found this syntax guide for Blat to be very useful.
Trying to connect to Google SMTP? — SMTP ERROR: ERROR-NOT A SOCKET
This is what I received after my attempt to use Blat from the command prompt. After some more research I found that because I was trying to connect to Google’s SMTP server, Google requires that the connection be made via SSL tunnel. More research time later, I discovered stunnel would potentially solve my problem via this guide. The guide had it spot on (where configuration was concerned), however, there were some rough spots I hit when installing and configuring stunnel.
Stunnel acts as a means to create an SSL tunnel between two machines. You can download stunnel here. I downloaded the executable offering “stunnel-4.56-installer.exe”.
To install stunnel:
1. Download and run the installer, agree to the license terms (if you want to of course).
2. When presented with the installation options, the only options to opt-into are “Terminal Version of stunnel” and “Start Menu Shortcuts”. An aside – you might not need the “terminal version” option, but those “start menu shortcuts” are necessary.
3. Choose a destination for the install.
To configure stunnel:
Locate the install directory that you specified, and edit “stunnel.conf”. Replace everything inside with the following (just as Mr. Kastner’s article says to do):
# GLOBAL OPTIONS client = yes output = stunnel-log.txt debug = 0 taskbar = no # SERVICE-LEVEL OPTIONS [SMTP Gmail] accept = 127.0.0.1:1099 connect = smtp.gmail.com:465 [POP3 Gmail] accept = 127.0.0.1:1109 connect = pop.gmail.com:995
Although I only needed to connect to Gmail’s SMTP server, I did as the article told me to and copied everything. The final step for setting up stunnel is installing stunnel as a Windows service, and to run the service.
To install stunnel as a service:
1. Navigate to “All Programs\Apps” (Start > All Programs or Windows Start Experience > All Apps in Windows 8).
2. Search for “stunnel” or navigate to the “stunnel” entry.
3. Execute the entry “stunnel Service Install”.
4. Once installed, execute “stunnel Service Start”.
Stunnel is now operational!
Sweet, sweet victory!
Finally, after all of the shenanigans, I tried Blat again and kabam! it worked on the first try. Below is a copy of the batch script that worked for me (you’ll need to fill in your info):
set SERVER=127.0.0.1:1099 set USER=YOUR_USERNAME_FOR_GMAIL set PW=YOUR_PASSWORD_FOR_GMAIL set FROMNAME=YOUR_NAME_AS_YOU_WANT_IT_TO_APPEAR_IN_INBOX set TO=TO_PERSON_1,TO_PERSON_2 set CC=CC_PERSON_1,CC_PERSON_2 set SUBJECT="A fanciful subject!" set BODY="Oh yes my dear lad!" blat -server %SERVER% -f %FROMNAME% -u %USER% -pw %PW% -to %TO% -cc %CC% -subject %SUBJECT% -body %BODY%
BlogSlayer is the official blog of FrogSlayer, a custom software product development shop in Bryan/College Station, Texas. Our specialty is getting your product to market in 90 days or less. If you would like a free consultation for your project or big idea, email us at firstname.lastname@example.org. You can also connect with us on Twitter, Facebook, or LinkedIn.