Simple Mail Transfer Protocol (SMTP) is a protocol to send electronic mail from one user to another user in a reliable and efficient manner. Mail servers such as Gmail, Outlook, etc use SMTP for sending an email. SMTP uses Transmission Control Protocol and runs on port number 25. For SMTP to work, both sender and receiver need not be online at the same time.
An SMTP session is initiated when a client opens a connection to a server by issuing the EHLO command and the server responds with an opening message. The SMTP session consists of commands originated by the SMTP clients and the responses from the SMTP Server. SMTP sessions are stateful. A session can contain zero or more SMTP transactions. Three important commands in SMTP transaction-
- MAIL – The transaction starts with the mail command. It does the sender identification.
- RCPT – It gives the receiver information. If RCPT command exists before previous MAIL command the n server throws 503 “Bad Sequence of Commands” response. This command can be issued multiple times.
- DATA – This command tells about the beginning of a message. Once accepted the SMTP server immediately replies with 354 responses and when the entire message is received SMTP server again replies with “250 OK”. SMTP indicates the end of the mail data by sending a line containing only a “.” (period or full stop).
The client who wants to send an email, the Mail client(or formally known as mail user agent or SMTP client) opens a full-duplex(two way) TCP connection with the Mail Server(SMTP Server) which is always in listening mode at the port number (25) and an SMTP session will be created. The Mail client uses TCP because it is a reliable ordered data stream channel for communication, hence the entire message will be delivered to the server without any data loss.
SMTP has 4 key components –
- Mail User Agent (MUA) – Mail User Agent or User-Agent is the client application for sending and receiving emails. Ex – Gmail, Microsoft Outlook, etc.
- Mail Submission Agent (MSA) – Mail Submission Agent is a server that receives mail from MUA, checks for errors, and in case of no errors pass it to the SMTP server. There exist Mail transfer agents that work as a mail submission agent too.
- Mail Transfer Agent (MTA) – Mail Transfer Agent receives the email from MSA or MUA(if MSA is not used) and passes the mail(using Simple Mail Transfer Protocol SMTP) to other MTA. An MTA works in the background, while the user can’t interact directly with a mail transfer agent. It will also find the MX record from the receivers’ DNS zone. Ex – Postfix, SendMail etc.
- Mail Delivery Agents (MDA) – Mail Delivery agents are responsible for delivering emails to recipients’ mailboxes.