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.)
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
You'll need the metamail package, so that you have the mimencode utility to base64 encode your login information.
# apt-get install metamail
Now we need to generate base64-encoded strings to use with the PLAIN and LOGIN methods.
# printf 'user@domain.com' | mimencode dXNlckBkb21haW4uY29t # printf 'password' | mimencode cGFzc3dvcmQ=
Those two are for the LOGIN method. The next one is for the PLAIN method. If you're impatient to just see if something works, this will gripe you less since it's only a single copy and paste. =)
# printf '\0user@domain.com\0password' | mimencode AHVzZXJAZG9tYWluLmNvbQBwYXNzd29yZA==
OK, let's telnet in:
# telnet localhost 25 Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. 220 mail.server.local ESMTP Postfix (Ubuntu)
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-PIPELINING 250-SIZE 10240000 250-VRFY 250-ETRN 250-STARTTLS 250-AUTH DIGEST-MD5 PLAIN LOGIN 250-AUTH=DIGEST-MD5 PLAIN LOGIN 250-ENHANCEDSTATUSCODES 250-8BITMIME 250 DSN
Alright. Good. Notice that we support three AUTH methods: DIGEST-MD5, PLAIN, and LOGIN. Let's try PLAIN first, using the string we generated for it above:
AUTH PLAIN AHVzZXJAZG9tYWluLmNvbQBwYXNzd29yZA== 235 2.0.0 Authentication successful quit 221 2.0.0 Bye
Excellent! If we want to try the LOGIN method, telnet back in and ehlo just as we did before, then:
AUTH LOGIN 334 VXNlcm5hbWU6 dXNlckBkb21haW4uY29t 334 UGFzc3dvcmQ6 cGFzc3dvcmQ= 235 2.0.0 Authentication successful quit 221 2.0.0 Bye
Again, excellent. (If you were curious, the 334 and 235 SMTP messages are also Base64 encoded, and decode to "Username:" and "Password:" when run through mimencode -u.)

