Sending mail via telnet

Sending mail via telnet through your SMTP server

There have been many times over the course of my time working with computers that I have had to manually test email functionality. Email is one of those things that everyone has to have for various reasons. It’s great for getting status info and results from scripts for example. However, it’s also one of those ornery things that is affected by everything from network settings and firewall rules to spam filters and DNS problems.

The point is that sometimes you need to be on the machine that is sending the message, and verify that it can in fact send a message. This article will help you not only test connectivity, but walk you through sending an email message via a telnet connection, going through the SMTP commands much like your mail client uses.

First, let’s understand that we are talking about email that is sent via SMTP (Simple Mail Transfer Protocol). This is a long time standard for sending mail, and I would say that the majority of mail servers and clients use it. There are exceptions, like Microsoft’s Exchange email server and Outlook client. It uses some proprietary mechanisms and even though you can turn on SMTP, this probably won’t help you if you are using Exchange.

On to SMTP, it’s important to note that SMTP uses port 25, if you didn’t already know that. So, a simple and quick test to see if you at least have basic connectivity, is to simply telnet to the target mail server from the one sending the message to port 25, like this:

telnet mail.servername.com 25

Which should return the greeting banner of the mail server you are trying to connect to if the connection is successful, which should look something like this:

220 mail.servername.com ESMTP server ready at Sun, 5 Aug 2007 18:57:40 -0500

Assuming the connection is successful, you can type quit to disconnect. If you don’t get a connection, you need to check elsewhere and see if a Firewall or some other setting is keeping you from talking to the mail server. Once you have verified that the connection can be made, you can further test by manually crafting an email via your telnet session. Check out the walk through of this operation below, with explanatory comments.

Telnet the the mail server, on port 25:
telnet mail.servername.com 25

Results:
Trying 10.10.10.10...
Connected to mail.servername.com.
Escape character is '^]'.
220 mx.servername.com ESMTP server ready at Sun, 5 Aug 2007 18:57:40 -0500

We give the mail server a greeting, by issuing the helo command along with the domain of the sender:
helo frommail.com

Results (the 10.10.10.10 I put here will be the address you are connecting from):
250 mail.servername.com Hello 10.10.10.10?, pleased to meet you

We tell the mail server who the mail is from:
mail from: me@frommail.com

Results:
250 2.1.0 me@frommail.com... Sender ok

We tell the mail server who the mail is for:
rcpt to: webmaster@servername.com

Results:
250 2.1.5 webmaster@servername.com... Recipient ok

We start the data portion of the mail, this would be where we insert any other headers we need (like Subject:, CC: or BCC:, etc.), as well as the text of the message:
data

Results:
354 Enter mail, end with "." on a line by itself

The rest of the data portion of our test message:
Subject: test message
This is a test!

Once complete, we put a single “.” on a line by itself to end the data input:
.

Results (the crazy number h5CH6GVI015806 is the message id, a unique identifier that you can find in the log files):
250 2.0.0 h5CH6GVI015806 Message accepted for delivery

We then issue the quit command to disconnect from the server:
quit

Results:
221 2.0.0 mx.servername.com closing connection
Connection closed by foreign host.

There you go! Does that all make sense to you? Not only do you now know how to send an email manually via telnetting to your mail server, you know what your mail client is going through when it sends a message! Cool, huh?

Let me recap those commands, as if you were just running through them blind, just to clarify and make an easy summation:

telnet mail.servername.com 25
helo frommail.com
mail from: me@frommail.com
rcpt to: webmaster@servername.com
data
Subject: test message
This is a test!
.
quit

It’s just that easy. Now, go out and give it a whirl for yourself. Enjoy!

One thought on “Sending mail via telnet

Tell me what you are thinking?