Testing SMTP server using telnet (whether with authentication or not)

You can need to perform testing with your SMTP server and see if you are able to send mail with it.
You have your domain name, and you want to check if mail server is answering properly?

1. Check the mail server with DNS entries

We first need to find the right DNS entry for MX record and know what is the mail server.

~$ nslookup
> server 8.8.8.8
Default server: 8.8.8.8
Address: 8.8.8.8#53
> set querytype=MX
> mydomain.com
Server:		8.8.8.8
Address:	8.8.8.8#53

Non-authoritative answer:
mydomain.com	mail exchanger = 10 mail.mydomain.com.

2. Open connection to mail server (default ports 25,587)

We will now open a telnet connection on the mail server we just found. Port to use can be different depending on the configuration of the server mail.

~$ telnet mail.mydomain.com 587
Trying www.xxx.yyy.zzz...
Connected to mydomain.com.
Escape character is '^]'.
220 mail.mydomain.com ESMTP Postfix

The answer can change depending on the mail engine used, this is just an example.

3. Send an email with or without authentication

In case of there is no authentication needs (an open smtp server), just perform:

EHLO mydomain.com 
250
MAIL FROM:<[email protected]> 
250 OK
RCPT TO:<[email protected]> 
250 OK
DATA
354 GO AHEAD
From: Me <[email protected]>
To: Him <[email protected]>
Subject: This is a testing mail
This message is only sent for testing purposes.
. 
250 OK 1413684421 qp 18534
QUIT
221

If the server requests an SSL/TLS authentication before being able to send mail, you will probably need to use openssl library as telnet does not support TLS by default (you will get an error on STARTTLS command).
You will also need to prepare the credentials by encoding them in base 64:

~$ echo -ne 'userpassword' | base64
AHVzZXIAcGFzc3dvcmQ=

Once you got this, you can open connection and request mail sending as following:

openssl s_client -starttls smtp -connect mail.mydomain.com:587
CONNECTED(00000003)
EHLO mydomain.com
250-mail.mydomain.com
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-AUTH PLAIN LOGIN
250-AUTH=PLAIN LOGIN
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
AUTH PLAIN AHVzZXIAcGFzc3dvcmQ=
235 2.7.0 Authentication successful
mail from: <[email protected]>
250 2.1.0 Ok
rcpt to: <[email protected]>
250 2.1.5 Ok
DATA
354 End data with .
From: Me <[email protected]>
To: Him <[email protected]>
Subject: This is a testing mail
This mail is only for testing purposes
.
250 2.0.0 Ok: queued as E65832403CF
QUIT
DONE

Notice that if the mail server doesn’t allow SMTP relay, you won’t be able to send a mail to another domain that the ones that this server is allowing. In that case, you will get an error as:

554 5.7.1 <[email protected]>: Relay access denied

That means you will only be able to send mail to an “@mydomain.com” mail address and not to any other.