Skip to main content

How to Test SMTP Server Connectivity in Batch Script

A script that sends email alerts is useless if the server can't communicate with the mail provider. SMTP (Simple Mail Transfer Protocol) connectivity can be blocked by firewalls, incorrect DNS settings, or ISP port blocking (especially Port 25). Before you deploy a monitoring script that relies on emails, you need a reliable way to verify the "Handshake" with the mail server. A Batch script can use PowerShell or Telnet to test if the specific mail port is open and responding correctly.

This guide will explain how to verify SMTP connectivity from the command line.

Method 1: Using PowerShell (Most Reliable)

PowerShell's Test-NetConnection is the industry standard for checking specific ports. It tells you not just if the server is "Up," but if the SMTP port itself is accepting traffic.

@echo off
setlocal

set "SMTPServer=smtp.gmail.com"
set "SMTPPort=587"

echo [TEST] Checking connection to %SMTPServer% on port %SMTPPort%...
echo.

:: Use PowerShell to test the TCP connection
powershell -NoProfile -Command ^
"$r = Test-NetConnection -ComputerName '%SMTPServer%' -Port %SMTPPort% -WarningAction SilentlyContinue -InformationLevel Quiet; if ($r) { exit 0 } else { exit 1 }"

if %errorlevel% equ 0 (
echo [SUCCESS] SMTP server is reachable and port is open.
) else (
echo [ERROR] Cannot reach SMTP server. Possible Firewall or Port blockage.
)

echo.
pause
endlocal

Method 2: The "Old School" Telnet Test

If your system has the Telnet Client enabled, you can perform a manual handshake to see the server's actual response message (e.g., "220 Service Ready").

@echo off
:: Note: This is an interactive test. The user must type 'quit' to exit.
echo [INFO] Opening Telnet connection...
telnet smtp.office365.com 587

Method 3: Using CURL (Modern Bridge)

curl can also be used to test SMTP connectivity and even verify if your credentials work.

@echo off
set "Server=smtp://mail.example.com:587"

:: --telnet-option = Use telnet protocol to verify connection
curl -v %Server% --connect-timeout 5

How to Avoid Common Errors

Wrong Way: Using "Ping" to test SMTP

A server might "Ping" successfully, but the SMTP service itself might be crashed, or the firewall might be blocking Port 25/587 explicitly.

Correct Way: Always test the specific port. A "Successful Ping" does NOT mean you can send email.

Problem: Port 25 Blocking

Most modern ISPs and cloud providers (Azure, Google Cloud, AWS) block Outbound Port 25 to prevent spamming.

Solution: If Port 25 fails, try Port 587 (most common for TLS) or Port 465 (SSL).

Best Practices and Rules

1. Identify "STARTTLS"

Many SMTP servers will connect but won't let you do anything until you issue the STARTTLS command. Use Method 3 (curl) with the -v (verbose) flag to see if the server is asking for an encrypted connection.

2. DNS Verification

If the connection fails, verify that your computer can resolve the hostname. nslookup smtp.provider.com

3. Check Local Firewall

Sometimes the blockage isn't at the ISP; it's your own Windows Firewall. Ensure powershell.exe has permission to make outbound connections on the specific SMTP port.

Conclusions

Testing SMTP connectivity is a vital "Pre-Flight" check for any automated system. By verifying that your script can "See" the mail server, you prevent "Silent Failures" where your monitors are triggering alerts that never actually reach your inbox. Whether you use PowerShell's modern connectivity tools or the classic Telnet handshake, this verification ensures your communication pipeline is robust and ready for production.