Authors Top

If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

1. Introduction

An SMTP server handles the sending and receiving of emails. Hence, its uptime is crucial.

In this tutorial, we’ll be looking at various methods that we can use to check if an SMTP server is up and running. 

2. Using telnet

Telnet is an application protocol that is used for interacting with remote hosts. We can use the telnet command to check the SMTP connection. In our case, we’ll be running an SMTP server locally. Therefore, our domain name is localhost.

Let’s use telnet to check if an SMTP server is running on port 2525:

[[email protected] ~]# telnet localhost 2525
Trying ::1...
telnet: connect to address ::1: Connection refused
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:

[[email protected] ~]# 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 will receive and relay it:

[[email protected] ~]# 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.

We see that the email was received by our 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 should see a “Connection refused” message:

[[email protected] ~]# telnet localhost 2525
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
telnet: connect to address 127.0.0.1: Connection refused

3. Using NetCat

The nc or ncat (NetCat) command is a networking utility for reading and writing data across networks. We can use this utility to test the connection of our SMTP server:

[[email protected] ~]# 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 simply add the -u option:

[[email protected] ~]# nc -vu localhost 2525
Ncat: Version 7.70 ( https://nmap.org/ncat )
Ncat: Connected to ::1:2525.

Additionally, we should see a “Connection refused” message if the SMTP server is inaccessible:

[[email protected] ~]# nc -v localhost 2525
Ncat: Version 7.70 ( https://nmap.org/ncat )
Ncat: Connection to ::1 failed: Connection refused.
Ncat: Trying next address...
Ncat: Connection refused.

4. Using nmap

The nmap command assists with network exploration. Although the intention of 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:

[[email protected] ~]# 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.
Other addresses for localhost (not scanned): ::1

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:

[[email protected] ~]# 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).
Other addresses for localhost (not scanned): ::1

PORT     STATE  SERVICE
2525/tcp closed ms-v-worlds

Nmap done: 1 IP address (1 host up) scanned in 0.27 seconds

5. Conclusion

In this article, we looked at several commands that can help us check if an SMTP server is reachable.

Moreover, we’ve explored sending test emails using one of the commands, telnet.

Authors Bottom

If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

Comments are closed on this article!