1. Introduction
A simple mail transfer protocol (SMTP) server handles the sending and receiving of emails. Hence, its uptime is crucial.
In this tutorial, we’ll be looking at various methods for checking whether an SMTP server is up and running.
2. Using telnet
Telnet is an application protocol for interacting with remote hosts. We can use the telnet command to check an SMTP connection.
In our case, we run a local SMTP server. Therefore, our domain name is localhost. Let’s use telnet to check if an SMTP server is running on port 2525:
$ telnet localhost 2525
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
220 a42502410059 smtp4dev ready
The connection is opened for a short while before it is terminated:
$ telnet localhost 2525
...
Connection closed by foreign host.
We can type a sample email while the connection is open and see if the SMTP server receives and relays it:
$ telnet localhost 2525
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
220 5cf184409a80 smtp4dev ready
helo user.sample.com
mail from:<[email protected]>
rcpt to:<[email protected]>
data
From: [email protected]
Subject: This is a test
Hi there :)
How are you doing?
.
quit
250 Nice to meet you
250 New message started
250 Recipient accepted
354 End message with period
250 Mail accepted
Connection closed by foreign host.
Consequently, we see that the email was received by the local SMTP server:
Session started. Client address 10.88.0.1.
Message received. Client address 10.88.0.1. From [email protected]. To [email protected].
Processing received message
Processing received message DONE
In contrast, when the SMTP server is unreachable, we usually see a Connection refused message:
$ telnet localhost 2525
Trying 127.0.0.1...
telnet: connect to address 127.0.0.1: Connection refused
Thus, we know that there either isn’t an SMTP service at port 2525 or that it’s not currently up and running.
3. Using Netcat
The nc or ncat (Netcat) command is a networking utility for reading and writing data across networks.
We can use Netcat to test the connection to an SMTP server:
$ nc -v localhost 2525
Ncat: Version 7.70 ( https://nmap.org/ncat )
Ncat: Connection to ::1 failed: Connection refused.
Ncat: Trying next address...
Ncat: Connected to 127.0.0.1:2525.
220 b9b103518a80 smtp4dev ready
The nc command can read or write data using either of the two protocols, TCP or UDP. The TCP protocol is the default protocol setting. Nevertheless, if we want to use the UDP protocol, we can just add the -u option:
$ nc -vu localhost 2525
Ncat: Version 7.70 ( https://nmap.org/ncat )
Ncat: Connected to 127.0.0.1:2525.
Additionally, we should see a Connection refused message if the SMTP server is inaccessible:
$ nc -v localhost 2525
Ncat: Version 7.70 ( https://nmap.org/ncat )
Ncat: Connection to 127.0.0.1 failed: Connection refused.
Ncat: Trying next address...
Ncat: Connection refused.
Again, the main way we distinguish an SMTP service from another one is the connection header or port.
4. Using nmap
The nmap command assists with network exploration. Although more often the intention behind the tool is to scan large networks, it can also scan single hosts. It’s important to note that there are legal issues regarding the tool – specifically, regarding the scanning of ports. Thus, it’s recommended that we use the tool only locally:
$ nmap localhost -p 2525
Starting Nmap 7.70 ( https://nmap.org ) at 2021-11-11 20:38 SAST
Nmap scan report for localhost (127.0.0.1)
Host is up.
PORT STATE SERVICE
2525/tcp filtered ms-v-worlds
Nmap done: 1 IP address (1 host up) scanned in 2.08 seconds
The purpose of the STATE column is to describe a port. The values for the column are open, filtered, and closed. The filtered state means that a firewall is in place. Furthermore, we can expect to see closed as the value of the STATE column when our SMTP server is inaccessible:
[root@localhost ~]# nmap localhost -p 2525
Starting Nmap 7.70 ( https://nmap.org ) at 2021-11-11 20:42 SAST
Nmap scan report for localhost (127.0.0.1)
Host is up (0.00010s latency).
PORT STATE SERVICE
2525/tcp closed ms-v-worlds
Nmap done: 1 IP address (1 host up) scanned in 0.27 seconds
This way, we know that port 2525 for the relevant service is closed, thereby rendering any potential SMTP server on the host inaccessible.
5. Using SWAKS
In addition to the more universal approaches, SWAKS provides the swaks tool hailed as the SWiss Army Knife for SMTP. In particular, swaks offers a number of benefits and features:
- TLS, authentication, pipeline, and PROXY support
- understands SMTP, ESMTP, and LMTP
- can work through UNIX sockets, IPv4 and IPv6 domain sockets, and pipes
- scriptable and flexible configuration (environment variables, files, and direct commands)
Let’s test our SMTP server via swaks:
$ swaks --to [email protected] --server localhost:2525
=== Trying localhost:2525...
=== Connected to localhost.
<- 220 localhost ESMTP Postfix (Debian/GNU)
-> EHLO localhost
<- 250-localhost
<- 250-PIPELINING
<- 250-SIZE 10240000
<- 250-VRFY
<- 250-ETRN
<- 250-STARTTLS
<- 250-ENHANCEDSTATUSCODES
<- 250-8BITMIME
<- 250-DSN
<- 250-SMTPUTF8
<- 250 CHUNKING
-> MAIL FROM:<smtpuser@localhost>
<- 250 2.1.0 Ok
-> RCPT TO:<[email protected]>
<- 250 2.1.5 Ok
-> DATA
<- 354 End data with <CR><LF>.<CR><LF>
-> Date: wed, 20 Mar 2024 20:03:24 +0000
-> To: [email protected]
-> From: smtpuser@localhost
-> Subject: test Wed, 20 Mar 2024 20:03:24 +0000
-> Message-Id: <20240320200324.001598@localhost>
-> X-Mailer: swaks v20201014.0 jetmore.org/john/code/swaks/
->
-> This is a test mailing
->
->
-> .
<- 250 2.0.0 Ok: queued as 1DD643FC9A
-> QUIT
<- 221 2.0.0 Bye
=== Connection closed with remote host.
Notably, Swaks sends a test mail on its own, thereby avoiding any more typing in addition to the command.
Further, the verbosity of the tool enables fairly simple debugging. For instance, let’s test another server:
$ swaks --to [email protected] --server smtp.gmx.de:25
=== Trying smtp.gmx.de:25...
=== Connected to smtp.gmx.de.
<- 220 gmx.net (mrgmx105) Nemesis ESMTP Service ready
-> EHLO xost
<- 250-gmx.net Hello localhost [46.10.18.57]
<- 250-8BITMIME
<- 250-SIZE 69920427
<- 250 STARTTLS
-> MAIL FROM:<smtpuser@localhost>
<** 530 Authentication required
-> QUIT
<- 221 gmx.net Service closing transmission channel
=== Connection closed with remote host.
Here, 530 Authentication required means we first need to authenticate to the server. Still, we can again employ swaks to perform this authentication:
$ swaks --to [email protected] --server smtp.gmx.de:25 --auth LOGIN
Username:
This way, we can provide credentials securely. When automating tasks, –auth-user (-au) and –auth-password (-ap) might be more convenient for supplying the credentials directly.
Naturally, if the server or service isn’t reachable, we get the respective response.
6. Online Tools
Although there are many local Linux-specific tools, we can also perform SMTP tests on the go from any Web browser.
For instance, the MXToolbox website offers an SMTP Diagnostics page for SMTP-specific tests:
While we can’t test a local SMTP server unless it’s properly secured and exposed on the Internet, this option provides a flexible way to get instant results without the hassle of installing or using platform-specific tools. Let’s see some example results:
Naturally, this is a fixed test and might not suit each scenario.
7. Conclusion
In this article, we looked at several commands that can help us check if an SMTP server is reachable or get further details about it. Moreover, we also saw some online tools for the same purose. Finally, we’ve explored sending test emails using one of the commands, telnet.