Test SMTP/IMAP(S)/POP3(S) configuration from the command line

In this post, I will show the full command line to test the SMTP/IMAP/POP3 server from the local command line.

SMTP server

There are 2 types of configuration, one requires STARTTLS and one does not.

A typical configuration has the following fields. Replace them with your configuration appropriately.

  • SMTP server: smtp.example.com
  • Port: 587
  • Username: transang@example.com
  • Password: mysecuredpassword
  • Require STARTTLS: true or false

Send an email via the SMTP server

You want to send a test email to the email test@gmail.com


If the SMTP server does not require STARTTLS

  • Step 1: telnet smtp.example.com 587
  • Step 2: type EHLO smtp.example.com then press Enter
  • Step 3: type AUTH LOGIN then press Enter
  • Step 4:
    (Optional) After the previous step, the server would return 334 VXNlcm5hbWU6
    (Optional) Open Javascript console, run command atob('VXNlcm5hbWU6') to make sure VXNlcm5hbWU6 is base64-decoded to Username: which means the server request for the username
    In Javascript console, run command btoa('transang@example.com') to get result dHJhbnNhbmdAZXhhbXBsZS5jb20=, which is a base64-encoded string of the username in your configuration
  • Step 5: go back to the telnet console, type dHJhbnNhbmdAZXhhbXBsZS5jb20=, then press Enter
  • Step 6: similar to step 4
    (Optional) After the previous step, the server would return 334 UGFzc3dvcmQ6 which means Password: after base64-encoded
    In Javascript console, run command btoa('mysecuredpassword') to get result bXlzZWN1cmVkcGFzc3dvcmQ=, which is a based64-encoded string of the password in your configuration
  • Step 7: go back to the telnet console, type bXlzZWN1cmVkcGFzc3dvcmQ=, then press Enter
  • Step 8:
    (Optional) Server would return 235 2.0.0 OK Authenticated after the previous step
    Type MAIL FROM: transang@example.com, then press Enter
  • Step 9:
    (Optional) server would return 250 2.1.0 transang@example.com... Sender ok
    Type RCPT TO: test@gmail.com, then press Enter
  • Step 10:
    (Optional) server would return 250 2.1.5 test@gmail.com... Recipient ok
    Type DATA, then press Enter
  • Step 11: type Subject: This is subject of the test email, then press Enter
  • Step 12: press Enter again
  • Step 13: Type content of the email body with or without the newline string. For example type This is body of the test email, then press Enter
  • Step 14: Finish and send an email by a single line with ., then press Enter
  • Step 15: type QUIT, then press Enter

Now open your inbox of the test email test@gmail.com to check if the test email has been sent


If the SMTP server requires STARTTLS.

For example Gmail server at smtp.gmail.com

  • Step 1: openssl s_client -starttls smtp -connect smtp.example.com:587
  • The following steps are the same as in the previous section (step 1 to step 15)

Q/A

  • What does the 3 digit-number mean at the beginning of messages from the server? For example 334, 235, 250.
    They are called SMTP codes. Refer to their meaning here: https://serversmtp.com/smtp-error/.
  • How to know if SMTP requires/supports STARTTLS?
    Following the procedure in the first section, i.e., pretend the server does not require STARTTLS.
    After step 3 (type AUTH LOGIN, then press Enter).
    If the server returns 334 VXNlcm5hbWU6, which means that the server does not require STARTTLS.
    If the server returns 530 5.7.0 Must issue a STARTTLS command first. e65sm6419106pfc.184 - gsmtp, which means that the server requires STARTTLS.
    Otherwise, the server returns other values. Please comment and let me know.
  • How to check which SSL cipher types does the server support?
    Use command nmap --script ssl-enum-ciphers -p 587 smtp.example.com
    or nmap --script ssl-enum-ciphers -Pn -p 587 smtp.example.com.
  • There is also a package named telnet-ssl which supports telnet via SSL channel.
    Use command telnet-ssl -z ssl smtp.example.com 465
    Then use ctrl-] menu to activate starttls if required.

Source:


Send an email to an SMTP server

  • Step 1: telnet smtp.example.com 587
  • Step 2: type HELO smtp.example.com then press Enter (Note: use HELO instead of EHLO)
  • Skip steps 3, 4, 5, 6, 7
  • Do all steps from step 8

Test IMAP server

The server does not have TLS/SSL

  • telnet imap.example.com 143
  • . USER <username> <password> (notice the leading dot)
  • . LIST "" "*" (notice the leading dot)
  • Ctrl + ] to quit

The server requires TLS/SSL

  • openssl s_client -connect imap.example.com:993
  • All steps are the same as above

Test POP3 server

The server does not have TLS/SSL

  • telnet pop3.example.com 110
  • USER <username>
  • PASS <password>
  • LIST
  • QUIT to quit

The server requires TLS/SSL

  • openssl s_client -connect pop3.example.com:995
  • All steps are the same as above