SMTP, testing via Telnet
From Ubuntuwiki.net
When troubleshooting problems with SMTP service - your own, or others - it is frequently very helpful to be able to "speak" to the SMTP server directly, rather than going through a mail client which won't necessarily tell you exactly what the SMTP server is saying. You can easily do this with the telnet client.
Note that many ISPs do not allow outbound connections on port 25 to any SMTP server but their own - if you get timeouts when trying to connect to port 25, you should try port 587, which is the standard ESMTP port. (Port 587 connections normally require SMTP AUTH, which is covered below.)
Contents |
Testing an SMTP service via telnet
ph34r# telnet localhost 25 Trying 127.0.0.1... Connected to localhost.localdomain. Escape character is '^]'. 220 ESMTP HELO justtesting 250 MAIL FROM: me@telnettingin.com 250 ok RCPT TO: postmaster@mail.getsdeliveredhere.net 250 ok DATA 354 go ahead To: postmaster@mail.getsdeliveredhere.net From: telnetclient@mail.getsdeliveredhere.net Subject: this is a test message Date: Thu, 21 Jun 2007 11:11:40 -0400 Just testing SMTP functionality by telnetting in to port 25. I'll end this message now by entering in a line with nothing but a period in it and hitting return. . 250 ok 1103093638 qp 87827 QUIT 221 Connection closed by foreign host.
Okay - our SMTP server just accepted a telnet connection, responded like a mailserver, and accepted a nice little test email for delivery. (Any response other than a 250 ok would represent an error of one sort or another.)
Testing SMTP AUTH via telnet
PLAIN method
First, we need to generate a base64-encoded string for the PLAIN method, in the form of [null]username[null]password :
# perl -MMIME::Base64 -e 'print encode_base64("\000user\@domain.com\000password");' AHVzZXJAZG9tYWluLmNvbQBwYXNzd29yZA==
IMPORTANT NOTE: notice that we escaped the '@' character with a backslash! If you don't, Perl will try to evaluate it as an array, and while you won't get an error, you won't get a usable hash, either.
NOTE: you can also use online encoding e.g. [1] though it's not very safe.
OK, let's telnet in:
ph34r# telnet localhost 25 Trying 127.0.0.1... Connected to localhost.privatedns.com. Escape character is '^]'. 220 mail.server.local ESMTP
Great, we got a banner. OK, now let's tell it we want to use extended SMTP with the "ehlo" command:
ehlo test 250-mail.server.local 250-AUTH LOGIN PLAIN 250-AUTH LOGIN PLAIN 250-STARTTLS 250-PIPELINING 250 8BITMIME
Alright. Good. Notice that we support two AUTH methods: LOGIN, and PLAIN. We'll issue an AUTH PLAIN command, using the string we generated for it above:
AUTH PLAIN AHVzZXJAZG9tYWluLmNvbQBwYXNzd29yZA== 235 ok, go ahead (#2.0.0)
Excellent! Our authentication proceeded just fine. If we would like, we can now send a message just as we would have in the regular SMTP session listed above; or we can simply quit now if all we wanted to test was the authentication itself.
LOGIN method
If we want to try the LOGIN method, we'll need to generate separate base64-encoded strings for the username and the password:
ph34r# perl -MMIME::Base64 -e 'print encode_base64("user\@domain.com");' dXNlckBkb21haW4uY29t ph34r# perl -MMIME::Base64 -e 'print encode_base64("password");' cGFzc3dvcmQ=
Now we telnet back in, ehlo as before, and this time authenticate using the LOGIN method:
AUTH LOGIN 334 VXNlcm5hbWU6 dXNlckBkb21haW4uY29t 334 UGFzc3dvcmQ6 cGFzc3dvcmQ= 235 2.0.0 Authentication successful
Again, excellent. (If you were curious, the 334 SMTP messages are also Base64 encoded, and decode to "Username:" and "Password:".) You can either send a test message with the same commands as in the plain SMTP example at the top, or you can simply quit if you just wanted to test the authentication.