2 February 2017

How to test an SMTP server by using telnet

What you need

  • The host name of the recipient’s mail exchange (for use in the telnet command). Use your favourite DNS lookup tool to determine it. Examples for dig and nslookup follow:
    • client: > dig example.com mx
      server: ; <<>> DiG 9.1.3 <<>> example.com mx
      server: ;; global options:  printcmd
      server: ;; Got answer:
      server: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 59376
      server: ;; flags: qr aa rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 2, ADDITIONAL: 4
      server: ;; QUESTION SECTION:
      server: ;example.com.                   IN      MX
      server: ;; ANSWER SECTION:
      server: example.com.            1800    IN      MX      10 mx1.example.com.
      server: example.com.            1800    IN      MX      20 mx2.example.com.
      server: ;; AUTHORITY SECTION:
      server: example.com.            1800    IN      NS      ns1.example.com.
      server: example.com.            1800    IN      NS      ns2.example.com.
      server: ;; ADDITIONAL SECTION:
      server: mx1.example.com.        3520    IN      A       192.0.2.2
      server: mx2.example.com.        3520    IN      A       192.0.2.3
      server: ns1.example.com.        60      IN      A       192.0.2.2
      server: ns2.example.com.        60      IN      A       192.0.2.3
      server: ;; Query time: 12 msec
      server: ;; SERVER: 192.0.2.4#53(192.0.2.4)
      server: ;; WHEN: Fri Jan 20 22:18:51 2017
      server: ;; MSG SIZE  rcvd: 148
      
    • client: > nslookup -query=mx example.com
      server: Server:  nameserver.example.com
      server: Address:  192.0.2.4
      server: Non-authoritative answer:
      server: example.com     preference = 10, mail exchanger = mx1.example.com
      server: example.com     preference = 20, mail exchanger = mx2.example.com
      
  • Your own host name (for use in the HELO command)
  • The sender’s email address (for use in the From: line and the MAIL command)
  • The recipient’s email address (for use in the To: line and the RCPT command)
  • An email message (to send after receiving a positive response to the DATA command)

What to do

The intial telnet: > symbolises your shell prompt.
The empty line between the headers and the body is important, as is the line containing only a full stop at the end of the article body.
telnet: > telnet mx1.example.com smtp
telnet: Trying 192.0.2.2...
telnet: Connected to mx1.example.com.
telnet: Escape character is '^]'.
server: 220 mx1.example.com ESMTP server ready Fri, 20 Jan 2017 22:33:36 +0200
client: HELO client.example.com
server: 250 mx1.example.com
client: MAIL from: <sender@example.com>
server: 250 Sender <sender@example.com> Ok
client: RCPT to: <recipient@example.com>
server: 250 Recipient <recipient@example.com> Ok
client: DATA
server: 354 Ok Send data ending with <CRLF>.<CRLF>
client: From: sender@example.com
client: To: recipient@example.com
client: Subject: Test message
client: 
client: This is a test message.
client: .
server: 250 Message received: 20170120203404.CCCC18555.mx1.example.com@client.example.com
client: QUIT
server: 221 mx1.example.com ESMTP server closing connection