Every day, we send and receive emails that involve a complex system despite the simplicity of the protocol. In this article, we will delve into the fundamental characteristics and operations of SMTP.

What is SMTP?

SMTP, or Simple Mail Transfer Protocol, is an application layer protocol responsible for transferring emails between servers and computer networks. The original model was introduced in 1982, and the SMTP client and SMTP server communicate using commands and responses. SMTP can only send simple messages, so the Multipurpose Internet Mail Extensions (MIME) protocol is used for attachments, non-English messages, and HTML/CSS formats.

ESMTP explained

ESMTP, or Extended Simple Mail Transfer Protocol, was introduced in 1995 to create a unified structure for future extensions. It uses a new EHLO command and allows additional parameters in SMTP's MAIL FROM and RCPT TO commands. ESMTP and SMTP are both widely used today.

Secure SMTPS

SMTPS, or Simple Mail Transfer Protocol Secure, secures SMTP with transport layer security (TLS) or Secure Sockets Layer (SSL) protocols. These security layers encrypt messages to prevent spammers or spoofers from viewing the contents of the emails.

Types of SMTP

There are four main types of SMTP systems: Originating SMTP, Delivery SMTP, Relay SMTP, and Gateway SMTP. The main components of the SMTP model are User, Sender-SMTP (SMTP client), and Receiver-SMTP (SMTP server). Mail agents, such as MUA (Mail User Agent), MSA (Mail Submission Agent), MTA (Mail Transfer Agent), and MDA (Mail Delivery Agent) also participate in the process of sending and receiving emails.

SMTP Relay process

SMTP relay is the process of transmitting emails between SMTP servers hosted on different domains. An SMTP session begins when the client opens a Transmission Control Protocol (TCP) connection to the SMTP server. The server responds with an opening message, and the client then sends a HELO command (EHLO for ESMTP) and identifies itself. The email transfer stage will then begin.

SMTP Queue systems

SMTP queue is a set of emails that are waiting to be delivered. Emails are usually put in the queue when the receiving SMTP server isn't ready to accept emails, or you're sending large volumes at once. SMTP commands and responses are used in the process of sending and receiving emails.

How SMTP networks function

SMTP (Simple Mail Transfer Protocol) is a communication protocol used for sending and receiving email messages over the internet. SMTP infrastructure includes SMTP servers, mail agents, and SMTP relay.

Overall, SMTP infrastructure works by using SMTP servers to send and receive email messages, mail agents to manage the messages, and SMTP relay to transfer the messages between different SMTP servers. Together, these components allow email messages to be sent and received across the internet in a reliable and efficient manner.

SMTP servers

SMTP servers are computers that are responsible for sending and receiving email messages. When an email message is sent, it is sent to an SMTP server that is responsible for delivering it to the recipient's mailbox. When an email message is received, it is received by an SMTP server and stored in the recipient's mailbox.

MTA mail agents

Mail agents are software programs that run on SMTP servers and are responsible for managing email messages. When an email message is received by an SMTP server, it is passed to a mail agent that is responsible for processing it. The mail agent checks the recipient's mailbox and delivers the message to the appropriate mailbox. Similarly, when an email message is sent, the mail agent is responsible for sending it to the SMTP server.

Mail AgentDescription
MUA (Mail User Agent)A software application used by end-users to manage their email messages. Also known as an email client, examples include Microsoft Outlook, Apple Mail, and Mozilla Thunderbird.
MSA (Mail Submission Agent)A software program responsible for accepting email messages from MUAs and submitting them to the MTA for delivery. MSAs typically listen on port 587 and use the SMTP protocol with STARTTLS encryption. Examples include OpenSMTPD and Postfix.
MTA (Mail Transfer Agent)A software program responsible for routing and delivering email messages between SMTP servers. MTAs use the SMTP protocol to transfer email messages and are typically configured as either inbound or outbound servers. Examples include Sendmail, Exim, and Qmail.
MDA (Mail Delivery Agent)A software program responsible for delivering email messages to the recipient's mailbox. MDAs typically store email messages in a mailbox format, such as mbox or Maildir. Examples include Procmail and Dovecot.

Relay SMTP

SMTP relay is the process of transferring email messages from one SMTP server to another. When an email message is sent, it is sent to the SMTP server of the sender's email provider. The SMTP server then sends the message to the SMTP server of the recipient's email provider. This process may involve one or more intermediate SMTP servers, which are responsible for relaying the message to the next SMTP server in the chain. SMTP relay is necessary to ensure that email messages can be sent and received across different email providers and networks.

SMTP commands and response codes

SMTP is a protocol based on commands and responses. The SMTP client sends commands to the SMTP server, and the server responds with a status code and message. Here are some of the most common SMTP commands and responses.

CommandDescription
HELO <domain>Identifies the sender's domain to the SMTP server.
EHLO <domain>Identifies the sender's domain to the SMTP server and requests extended features.
MAIL FROM: <address>Specifies the sender's email address.
RCPT TO: <address>Specifies the recipient's email address.
DATAIndicates the start of the email data.
QUITTerminates the SMTP session.
AUTHInitiates an authentication process with the SMTP server.
NOOPDoes nothing, but can be used as a keep-alive message.
RSETResets the current mail transaction.
HELPRequests help information from the SMTP server.
VRFYVerifies the existence of an email address.
EXPNExpands a mailing list.
TURNSwitches the SMTP session to reverse mode.

Commands example

Open the command prompt (Windows) or terminal (Mac/Linux) and type the following command to connect to the SMTP server:

Replace smtp.server.com with the actual SMTP server address.

After establishing the connection, type the following commands to start the email transaction and identify yourself as the sender:

Replace yourdomain.com with your actual domain name and sender@example.com with the email address of the sender.

Type the following command to specify the recipient's email address:

Replace recipient@example.com with the email address of the recipient.

Type the following command to start the data transaction:

Type the following commands to specify the email subject and body:

Note that there should be a blank line after the To: field.

Type a period . on a new line to indicate the end of the email data:

Type the following command to close the connection and send the email:

If everything is successful, the email should be sent and delivered to the recipient's inbox.

SMTP response codes

When you issue an SMTP command the server replies with a response status code. Here are some common codes and what they mean.

CodeResponse MessageDescription
220Service readyThis response indicates that the SMTP server is ready to receive commands.
250Requested mail action okay, completedThis response indicates that the requested action was successful.
251User not local; will forward to <forward-path>This response indicates that the recipient's mailbox is not hosted on the current server and will be forwarded to another server.
354Start mail input; end with <CRLF>.<CRLF>This response indicates that the server is ready to receive the email data.
421Service not available, closing transmission channelThis response indicates that the SMTP server is not available or is unable to process the request at the moment.
450Requested mail action not taken: mailbox unavailableThis response indicates that the recipient's mailbox is unavailable.
451Requested action aborted: local error in processingThis response indicates that the server encountered an error while processing the request.
452Requested action not taken: insufficient system storageThis response indicates that the server is running low on storage space and cannot process the request.
500Syntax error, command unrecognizedThis response indicates that the SMTP command is not recognized by the server.
501Syntax error in parameters or argumentsThis response indicates that there is a syntax error in the parameters or arguments of the SMTP command.
502Command not implementedThis response indicates that the SMTP command is not implemented by the server.
503Bad sequence of commandsThis response indicates that the SMTP command was issued in the wrong order or context.
504Command parameter not implementedThis response indicates that the specified command parameter is not implemented by the server.
550Requested action not taken: mailbox unavailable or not localThis response indicates that the recipient's mailbox is unavailable or does not exist.
551User not local; please try <forward-path>This response indicates that the recipient's mailbox is not hosted on the current server and should be forwarded to another server.
552Requested mail action aborted: exceeded storage allocationThis response indicates that the server has exceeded its storage allocation and cannot process the request.
553Requested action not taken: mailbox name not allowedThis response indicates that the recipient's mailbox name is not allowed or is invalid.
554Transaction failedThis response indicates that the SMTP transaction failed for some reason, such as an invalid email address or spam detection.

SMTP Ports

SMTP ports are communication endpoints that help identify the exact location of internet addresses. The most common ports used with SMTP are 25, 465, 587, and 2525.

PortDescription
25This is the default SMTP port used for email delivery. It is used for outbound email from a mail client to an SMTP server.
465This is the port used for SMTP over SSL (SMTPS). It is a secure version of SMTP that encrypts the connection between the email client and the server.
587This is the port used for SMTP submission. It is used for inbound email to an SMTP server from a mail client. This port supports the use of STARTTLS encryption.
2525This is an alternate port used for SMTP. It is sometimes used by ISPs and web hosting providers as an alternative to port 25.

SMTP vs IMAP vs POP3

SMTP is an outbound email server used for sending and delivering emails, while IMAP and POP3 are message access protocols used for retrieving incoming messages from the email server. Most programming languages allow users to route outgoing emails through SMTP either natively or with the help of different libraries.

IMAP telnet example

IMAP is often preferred over POP3 because it allows for email messages to be stored on the server, which enables better synchronization between email clients and the server, supports advanced features, and reduces storage requirements on the client device.

Here is an example using telnet:

POP3 telnet example

While POP3 is a simpler protocol than IMAP, it has some advantages in certain situations. For example, POP3 allows users to download email messages to a single device and manage them locally, which can be useful for users with limited internet connectivity or storage capacity. Additionally, POP3 can be faster than IMAP for retrieving email messages since it does not require continuous synchronization with the email server. However, overall, IMAP is usually considered to be a more versatile and feature-rich protocol than POP3.

Here is an example of POP3 using telnet:

Sending Emails with SMTP

Sending email with SMTP can be achieved in many ways as SMTP is simply protocol. That means you can use any programming language or library to send emails with SMTP. For example, you can use the JavaMail API to send emails with SMTP in Java. Or you can use the Nodemailer library to send emails with SMTP in Node.js.

Windows SMTP example

Here is an example using powershell on Windows:

Mac SMTP example using ZSH and sendmail

Open the Terminal app on your Mac.

Type the following command to open a text editor and create a new email message:

In the text editor, enter the email message you want to send, including the subject and body of the email.

Save the message and exit the text editor by pressing Ctrl + X, then Y, and finally Enter.

Type the following command to send the email using the sendmail command:

Replace recipient@example.com with the email address of the recipient.

If prompted, enter your email password to authenticate with the SMTP server.

If the email is successfully sent, you should see a message similar to the following:

SMTP on linux with Python

Here is an example using Python on Linux:

Save the file as then run in a terminal to send your email.

SMTP email services for testing

SMTP as a service is provided for two main purposes: testing and sending. In terms of testing, SMTP service implies a fake SMTP server that captures outgoing mail. When it comes to email sending, SMTP service is synonymous with SMTP relay service.

MailSlurp is an Email Delivery Platform that combines Email Testing and Email Sending. It's a complete email infrastructure that allows users to test SMTP servers, send emails with it, and monitor their performance with actionable analytics.