As a Linux administrator, you may find it necessary to troubleshoot or test your Simple Mail Transfer Protocol (SMTP) server to ensure it is working correctly. One of the most effective ways to do this is to use the telnet command.
Telnet allows you to manually connect to an SMTP server, send commands, and observe live responses from the server, helping you diagnose problems or verify configurations. This tutorial will guide you through the process of testing the SMTP server using the telnet command.
Step 1: Open a Telnet session
To start testing the SMTP server, open a Terminal on the Linux system and start a Telnet session to the SMTP server.
telnet smtp.example.com 25
Replace smtp.example.com equal to the domain or IP address of the SMTP server and 25 equal to the appropriate port number if different.
Step 2: Understand the SMTP server's greeting
Once connected, the SMTP server will respond with a hello message, typically displaying the server's hostname and indicating that the server is ready to receive commands. This message will look like this:
220 smtp.example.com ESMTP Postfix
If you receive this message, the connection was successful and the SMTP server is ready to accept commands.
Step 3: Start an SMTP conversation
EHLO command: Start a conversation by identifying yourself to the SMTP server. Use the EHLO command followed by domain or any placeholder like localhost.
EHLO localhost
The server will respond with a list of supported extensions and features:
250-smtp.example.com Hello localhost [127.0.0.1]
250-SIZE 10485760
250-PIPELINING
250-STARTTLS
250-ENHANCEDSTATUSCODES
250-8BITMIME
250-DSN
250 SMTPUTF8
HELO command: Alternatively, you can use the older HELO command instead of EHLO. However, EHLO is recommended because it indicates support for extended features.
HELO localhost
The response is usually simpler:
250 smtp.example.com Hello localhost [127.0.0.1]
Step 4: Specify the sender
Next, specify the sender's email address using the MAIL FROM command:
MAIL FROM:
The server will confirm the command:
250 2.1.0 Ok
Step 5: Specify recipients
Now, specify the recipient's email address using the RCPT TO command:
RCPT TO:
If the recipient is accepted, you'll see:
250 2.1.5 Ok
If the recipient is invalid or not accepted by the server, you may see an error like this:
550 5.1.1 : Recipient address rejected: User unknown in local recipient table
Step 6: Send message data
To send email data, use the DATA command:
DATA
The server will respond with a message indicating that it is ready to receive data:
354 End data with .
Now you can enter the email content. Start with the title:
Subject: Test Email
From: sender@example.com
To: recipient@example.com
This is a test email sent using Telnet.
After writing the email, end the data entry by entering a period (.) on a new line and pressing Enter:
.
The server will respond:
250 2.0.0 Ok: queued as ABC123DEF456
This indicates the email has been accepted and queued for delivery.
Step 7: End session
To end the SMTP session, use the QUIT command:
QUIT
The server will close the connection:
221 2.0.0 Bye
Step 8: Interpretation of common SMTP responses
During your interaction with the SMTP server, you will receive many different response codes. Here are some common response codes:
- 220: Server is ready.
- 250: Mail action requested, completed.
- 354: Start importing mail; ends with ..
- 421: Service not available, close channel.
- 450: Requested mail delivery action not taken: Mailbox unavailable.
- 550: Requested action not taken: Mailbox unavailable.
Testing SMTP servers using Telnet is a valuable skill for any Linux administrator. It allows you to manually send commands and observe the server response, making it easier to diagnose problems such as connection problems, incorrect SMTP configuration, or problems sending and receiving emails. While Telnet is useful for testing, remember that it transmits data in plain text, so it should only be used in a controlled, secure environment. For production use, always ensure that your SMTP communications are encrypted using STARTTLS or other secure protocols.