Everyone is not as lucky as having a full fletched email client like thunderbird or kmail to send mails. There is one unlucky group known as system administrators who have to send the mails either through the command line or a script running on the remote server. Also, apart from sending the emails, sometimes one needs to test or debug the email server which can’t be done by traditional email clients. If you are one of those system administrators and are scared, then you shouldn’t be, because this is where netcat comes to rescue.

Netcat( /usr/bin/nc ) is a simple utility which is used with TCP/UDP connections, sometime to troubleshoot and sometime as a bridge to interact with them. In our case, if we would like to send a email through port 25, we should be able to feed the stream of data containing the mail information to port 25, and that is what netcat is for.

Before starting with netcat we should know that what input an SMTP server expects on port 25, so that it could forward it as a mail to the designated SMTP server. We are going to confirm this by connecting to the SMTP server with telnet command on port 25 and issuing the commands and checking the response we are getting.

# telnet smtp.geekride.com 25
Trying 192.168.0.10...
Connected to smtp.geekride.com (192.168.1.1).
Escape character is '^]'.
220 smtp.geekride.com ESMTP
HELO smtp.geekride.com
250 smtp.geekride.com Hello [192.168.1.1], pleased to meet you
MAIL FROM: geekride@sender.com
250 2.1.0 geekride@sender.com... Sender ok
RCPT TO: geekride@gmail.com
250 2.1.5 geekride@gmail.com... Recipient ok
DATA
354 Enter mail, end with "." on a line by itself
From: [Geek Ride] <geekride@sender.com>
To: [Geek Ride] <geekride@gmail.com>
Date: Sat, 22 May 2010 07:43:25 -0400
Subject: Test Message

This is a test mail.

~GeekRide

.
250 2.0.0 o4MAd6I5006285 Message accepted for delivery
QUIT
221 2.0.0 smtp.geekride.com closing connection
Connection closed by foreign host.

Just replace the values with the one suited for you in the above example like the SMTP server to connect, receiving mail id. For the sender email-id, you don’t need to give a valid id but just needs to provide a valid domain name that is allowed to relay from the SMTP server. After finishing writing the mail enter “.” in a new line to send the mail.
Now we know that what input the remote server is expecting from us, and what we need to do is to put all the data in a text file and pass it to port 25 through netcat.
One more thing to notice is that the recipient mail server expects the date in specific format, so to get the date you are looking for use this command:

# date '+%a, %d %b %Y %H:%M:%S %z'
Mon, 12 Apr 2010 14:21:26 -0400

Now we just need to put all the data we input through telnet into a text file, which will look something like this.

HELO smtp.geekride.com
MAIL FROM: geekride@sender.com
RCPT TO: geekride@gmail.com
DATA
From: [Geek Ride] <geekride@sender.com>
To: [Geek Ride] <geekride@gmail.com>
Date: Sat, 22 May 2010 07:43:25 -0400
Subject: Test Message

This is a test mail.

~GeekRide

.
QUIT

Now just feed the file to the SMTP server via netcat.

# /usr/bin/nc jetwebpro.com 25 < /home/manishk/mail.txt
220 smtp.geekride.com ESMTP Sendmail 8.13.8/8.13.8; Sun, 23 May 2010 02:12:00 -0400
250 smtp.geekride.com Hello [192.168.1.1], pleased to meet you
250 2.1.0 geekride@sender.com... Sender ok
250 2.1.5 geekride@gmail.com... Recipient ok
354 Enter mail, end with "." on a line by itself
250 2.0.0 o4N6C0xd014823 Message accepted for delivery
221 2.0.0 smtp.geekride.com closing connection

And your mail is in the inbox. So, i think the process is pretty much clear over here. We have formatted the data to look like an email, then feed it through the netcat to the port 25 of the SMTP server and we are done.
Well, this is the simplest form of mail you could send through netcat. Obviously there are other ways and tools to do the same thing, and may be better than this, but this is just one way i am showing. If you devote some time, the email body can be generated through a small shell/perl script and with the help of cron that could send periodic details about your system (by running few commands) to a list of recipients.

References:

  1. Netcat
If you enjoyed this post, make sure you subscribe to my RSS feed!!!!