How to Test SMTP Server Connection Using Command Line Tools in Linux


2 views

When you need to verify SMTP server credentials and connectivity, Linux offers several command-line approaches:

telnet smtp.example.com 25
HELO test.example.com
MAIL FROM: <test@example.com>
RCPT TO: <recipient@domain.com>
DATA
Subject: SMTP Test
This is a test email sent via telnet.
.
QUIT
openssl s_client -connect smtp.example.com:465 -crlf -quiet
EHLO example.com
AUTH LOGIN
[base64-encoded username]
[base64-encoded password]
MAIL FROM: <sender@example.com>
RCPT TO: <recipient@domain.com>
DATA
Subject: OpenSSL SMTP Test
Testing encrypted SMTP connection.
.
QUIT

First install swaks:

sudo apt-get install swaks  # Debian/Ubuntu
sudo yum install swaks      # CentOS/RHEL

Then test SMTP:

swaks --to recipient@domain.com \
      --from sender@example.com \
      --server smtp.example.com \
      --port 587 \
      --auth LOGIN \
      --auth-user username \
      --auth-password password \
      --tls
import smtplib

try:
    server = smtplib.SMTP('smtp.example.com', 587)
    server.starttls()
    server.login('username', 'password')
    msg = "Subject: SMTP Test\n\nThis is a test email."
    server.sendmail('sender@example.com', 'recipient@domain.com', msg)
    print("Email sent successfully!")
except Exception as e:
    print(f"Error: {e}")
finally:
    server.quit()
  • Connection timeout: Verify port number and network connectivity
  • Authentication failed: Double-check credentials and authentication method
  • TLS/SSL errors: Ensure correct port (465 for SSL, 587 for STARTTLS)
  • Relay denied: Check if server allows relaying from your IP

When you receive SMTP server credentials, verifying their functionality is crucial before integrating them into your applications. While GUI tools exist, command-line testing offers more control and is preferred in development environments.

The most fundamental way to test SMTP involves telnet. While you mentioned it seems complicated, let's break it down:

telnet your.smtp.server.com 25
Trying 192.168.1.1...
Connected to your.smtp.server.com
220 smtp.server.com ESMTP Postfix
EHLO yourdomain.com
250-smtp.server.com
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-STARTTLS
250-AUTH PLAIN LOGIN
250-AUTH=PLAIN LOGIN
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN

For modern SMTP servers requiring TLS:

openssl s_client -connect your.smtp.server.com:465 -quiet
depth=2 C = US, O = DigiCert Inc, OU = www.digicert.com, CN = DigiCert Global Root CA
verify return:1
220 smtp.server.com ESMTP ready
AUTH LOGIN
334 VXNlcm5hbWU6
(base64-encoded-username)
334 UGFzc3dvcmQ6
(base64-encoded-password)
235 2.7.0 Authentication successful

For a more user-friendly approach, install and use swaks:

swaks --to recipient@example.com \
      --from sender@yourdomain.com \
      --server your.smtp.server.com \
      --port 587 \
      --auth LOGIN \
      --auth-user your_username \
      --auth-password your_password \
      --tls

Here's a complete bash script to test SMTP connectivity:

#!/bin/bash

SMTP_SERVER="your.smtp.server.com"
PORT=587
USERNAME="your_username"
PASSWORD="your_password"
RECIPIENT="test@example.com"

echo "Testing SMTP connection to $SMTP_SERVER..."

if ! nc -z -w5 $SMTP_SERVER $PORT; then
    echo "Connection to $SMTP_SERVER:$PORT failed"
    exit 1
fi

echo "Connection successful. Testing authentication..."

swaks --to $RECIPIENT \
      --from $USERNAME \
      --server $SMTP_SERVER \
      --port $PORT \
      --auth LOGIN \
      --auth-user $USERNAME \
      --auth-password $PASSWORD \
      --tls \
      --quit-after AUTH

if [ $? -eq 0 ]; then
    echo "SMTP authentication successful"
else
    echo "SMTP authentication failed"
fi

When testing fails, check these common problems:

  • Firewall blocking outbound port 25/465/587
  • Incorrect authentication method (PLAIN vs LOGIN)
  • Server requires STARTTLS on port 587
  • Credentials containing special characters needing escaping