Testing SMTP
SMTP is a very simple, and elegant protocol, used to transfer email messages between networks, to potentially multiple recipients. SMTP primarily operates over port 25. This is always true for inbound mail, ie, mail destined to a MX, but alternate ports are sometimes used for end users to send mail outbound into the Internet (ie submissions port 587).
Connecting to the mail server
First, we must determine which mail server(s) are responsible for accepting mail for a given domain. These servers are called the MX, or Mail eXchange. We utilize DNS to determine the declared MXs for a given domain:
$ dig mx gmail.com +short 40 alt4.gmail-smtp-in.l.google.com. 5 gmail-smtp-in.l.google.com. 10 alt1.gmail-smtp-in.l.google.com. 20 alt2.gmail-smtp-in.l.google.com. 30 alt3.gmail-smtp-in.l.google.com.
We receive a list of hostnames and associated priority numbers. The lower the number, the higher the priority. It appears that we should connect to gmail-smtp-in.l.google.com first:
$ telnet gmail-smtp-in.l.google.com 25 Trying 209.85.223.9... Connected to gmail-smtp-in.l.google.com. Escape character is '^]'. 220 mx.google.com ESMTP 9si8465377iwn.40
Great. We’ve connected to GMail’s inbound mail exchange, and have received a 220 response code. In SMTP talk, a 2XX series return code indicates something successful has happened. In our case, we’ve successfully connected to the mail server.. hooray!
Now, let’s say hi to the server:
ehlo pete.com 250-mx.google.com at your service, [216.40.38.232] 250-SIZE 35651584 250-8BITMIME 250-ENHANCEDSTATUSCODES 250 PIPELINING
I typed ehlo pete.com (note the spelling: EHLO) and the server has replied with a multiline response of 250 (again, something successful happened!). The lines starting with “250-” indicate that a subsequent status line shall follow. The line with “250 SOMETEXTHERE” indicates that this is the last line that the server will send.
We see that the server support 35MB message sizes, 8bitmime, enhancedstatuscodes and pipelining. Great. Let’s continue with our transaction:
mail from:<pete@REDACTED> 250 2.1.0 OK 9si8465377iwn.40
Great. I’ve told it that I ( pete@REDACTED ) am sending it some mail (note that REDACTED should be a valid domain). It has indicated with a 250 that it likes me, and that I should continue.
rcpt to:<petermblair@REDACTED> 250 2.1.5 OK 9si8465377iwn.40
“rcpt to” indicates which user on that system I would like to deliver mail to. I can supply multiple recipients by giving multiple calls to “rcpt to”.
data 354 Go ahead 9si8465377iwn.40
Aha, a “354″ response. This is an information response. It’s telling us that we should “go ahead”. Very informative. Thanks.
Subject: hi there From: Pete This is a sample/test message . 250 2.0.0 OK 1259959810 9si8465377iwn.40
Everything but the last line was entered by myself. I terminated the message by submitting a “.” on the line by itself. No trailing whitespace. The server responded with a 250 (again, success!) indicating that it has accepted my mail. That message should now be present in my inbox or spam folder, depending on the content of the message.
Testing Outbound Mail
This will continue soon, showing how to authenticate against a mail server.