Batch File or VBScript: Test SMTP

Hi there,

I need to make a batch file that will test an SMTP server if it responds to the helo command.

Please find below an example of what I would like to do.

I would prefer that this script would be as a batch file. However, if it's not possible, ok for vbscript.

If it is in VBScript, it will be run within a batch file with the cscript command and the output will have to be the response the the smtp server.

Thanks for your help,
Rene

 
@ECHO OFF
REM TEST FROM BATCH FILE
:Home1
TELNET smtp.server.com 25 hello | Findstr -i "pleased to meet you"
IF %errorlevel% NEQ 0 (ECHO SMTP DOWN) ELSE (ECHO SMTP UP)
REM WAIT 30 SECONDS
CHOICE /D Y /T 30 >NUL
GOTO Home1

Open in new window

@ECHO OFF
:Home2
REM TEST FROM VBSCRIPT FROM WITHIN A BATCH FILE
cscript TestSMTP.vbs smtp.server.com 25 | Findstr -i "pleased to meet you"
REM (where 25 is the port number) 
IF %errorlevel% NEQ 0 ECHO SMTP Server is down
REM WAIT 30 SECONDS
CHOICE /D Y /T 30 >NUL
GOTO Home2

Open in new window

LVL 10
ReneGeAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

andrevanzuydamCommented:
Hi Rene

I'm not sure if this presents itself as a solution but telnet and various other tools like plink are pretty slow to connecting to a server port to test.  Instead I would opt for downloading portqry from Microsoft to assist with your batch file.  Please note that telnet needs to be selected in Programs & Features in Windows 7.  This is also smaller and doesn't rely on active x components which a VB script would.  The 220 is the code/ status you would need to check if the SMTP port is active and ready.

@ECHO ON
REM TEST FROM BATCH FILE
:Home1
PortQry -n smtp.server-e 25 | Findstr -i "220"
IF %errorlevel% NEQ 0 (ECHO SMTP DOWN) ELSE (ECHO SMTP UP)
REM WAIT 30 SECONDS
CHOICE /D Y /T 30 > NUL
GOTO Home1

Open in new window


Download Link for PortQry
http://www.microsoft.com/downloads/details.aspx?familyid=89811747-C74B-4638-A2D5-AC828BDC6983&displaylang=en

ReneGeAuthor Commented:
Thanks andrevanzuydam

Our ISP SMTP server was indeed responding to the portqry command buy the moment I was using telnet and sending the command helo, their SMTP server was closing the connection.

While monitoring, I did not test if 220 was included in the output. My test was simply:
PortQry -n smtp.server-e 25
IF %errorlevel% NEQ 0 ECHO ISP SMTP SERVER CRASHED.


With what I shared with you, do you think that I would have detected the ISP SMTP bug by testing if 220 is included in the output?

Thanks and cheers,
Rene
andrevanzuydamCommented:
Hi Rene

Well, I would have a look at the generic SMTP status codes, even if you get the 220 back, it does not mean that you would be able to send out emails.  You may want to exend your batch file a bit more to handle more error codes ?

But yes, I think it is safe to say you would have done better testing for the 220 as well, I can site examples of this where one will get a response from portqry but the server is actually not accepting connections to send email. (other codes instead of 220 will be returned)

On Sendmail which I use sometimes the ports are open but the server has received too many connections and it "hibernates" for a while before receiving connections again.

Hope this answers your question.

Andre

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ReneGeAuthor Commented:
Hey Andre,

Well, by this thread, I'v realise that I need to modify my script to not only test the execution error code but rather it's output.

If I have new challanges in this regards, I'll create a new thread.

Thanks for your help and kindness.

Cheers,
Rene
andrevanzuydamCommented:
No problems, I like these kind of problems

Cheers

Andre
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Scripting Languages

From novice to tech pro — start learning today.