Troubleshooting with netcat

netcat is the swiss-army knife of networking. It is an extremely valuable tool to help diagnose any networking errors. You can and should use it often when working on sysadmin and writing code.

Debugging SMTP

You can use netcat to test for SMTP errors such as open mail relays.

Here's how to send a simple letter:

$ nc ircnow.org 25 
220 ircnow.org ESMTP OpenSMTPD

Next, we type HELO followed by our sending domain:

HELO example.com
250 ircnow.org Hello example.com [38.81.163.143], pleased to meet you

Afterwards, we type our sending mail address:

MAIL FROM: <test@example.com>
250 2.0.0 Ok

And the destination mail address:

RCPT TO: <jrmu@ircnow.org>
250 2.1.5 Destination address valid: Recipient ok

Then we type DATA followed by our email:

DATA
354 Enter mail, end with "." on a line by itself
Subject: Alpha Bravo Charlie Delta

Echo Foxtrot Golf Hotel

We then type . to end the email, then QUIT:

.
250 2.0.0 e57f9a36 Message accepted for delivery
QUIT
221 2.0.0 Bye

Here's the complete process:

$ nc ircnow.org 25 
220 ircnow.org ESMTP OpenSMTPD
HELO example.com
250 ircnow.org Hello example.com [38.81.163.143], pleased to meet you
MAIL FROM: <test@example.com>
250 2.0.0 Ok
RCPT TO: <jrmu@ircnow.org>
250 2.1.5 Destination address valid: Recipient ok
DATA
354 Enter mail, end with "." on a line by itself
Subject: Alpha Bravo Charlie Delta

Echo Foxtrot Golf Hotel
.
250 2.0.0 e57f9a36 Message accepted for delivery
QUIT
221 2.0.0 Bye