Cloud VPS from $1.98/mo · pay with Bitcoin, Ethereum, Monero, USDT or Solana · paid in crypto
Networking

SMTP, IMAP and POP3 explained: ports, TLS and tests

How SMTP, IMAP and POP3 move email, which ports use implicit TLS or STARTTLS, where JMAP fits, and openssl and curl commands to test each one from a server.

CloudSat teamNovember 18, 20267 min read
Abstract illustration of an envelope traveling across three connected orbits, each lit in a different color
On this pageThree trips, three jobs

Every email makes at least three trips. A mail client or an application submits it to an outgoing server, that server relays it to the recipient's server, and the recipient's client reads it from a mailbox. SMTP handles the first two trips; IMAP or POP3 handles the last. When mail goes missing, the first question is which trip failed, and each trip has its own ports, encryption mode and test. This guide maps them and gives a command for each.

Three trips, three jobs

Trip Protocol Ports Encryption Standard
Client or app to its outgoing server SMTP submission with login 587, 465 STARTTLS on 587, implicit TLS on 465 RFC 6409, RFC 8314
Server to server SMTP relay 25 Opportunistic STARTTLS RFC 5321, RFC 3207
Mailbox kept on the server IMAP 993, 143 Implicit TLS on 993, STARTTLS on 143 RFC 9051
Mailbox downloaded to a device POP3 995, 110 Implicit TLS on 995, STLS on 110 RFC 1939, RFC 2595
Mailbox over HTTPS JMAP 443 HTTPS RFC 8620, RFC 8621

Implicit TLS encrypts the connection from the first byte. STARTTLS connects in plaintext, then upgrades after a command. RFC 8314 recommends implicit TLS for submission and mailbox access, but both are safe when the client insists on encryption. STARTTLS is weak only when the client accepts a plaintext fallback: an attacker in the path can delete the server's STARTTLS offer, and a lenient client carries on unencrypted.

SMTP: one conversation, two sets of addresses

SMTP is a plain-text dialogue of commands and numbered replies. A delivery looks like this:

S: 220 mx.example.org ESMTP
C: EHLO app.example.com
S: 250-mx.example.org
S: 250-STARTTLS
S: 250 SIZE 52428800
C: MAIL FROM:<billing@example.com>
S: 250 2.1.0 Ok
C: RCPT TO:<alice@example.org>
S: 250 2.1.5 Ok
C: DATA
S: 354 End data with <CR><LF>.<CR><LF>
C: From: Billing <billing@example.com>
C: To: alice@example.org
C: Subject: Invoice 1042
C:
C: Your invoice is ready.
C: .
S: 250 2.0.0 Ok: queued as 4Xb2kL
C: QUIT
S: 221 2.0.0 Bye

Two kinds of address appear. MAIL FROM and RCPT TO form the envelope: where bounces go and who actually receives the message (a Bcc recipient exists only here). The From: and To: lines after DATA are headers, the part people see. SPF checks the envelope sender's domain; DMARC requires the visible From: domain to align with what SPF or DKIM verified. Many "why is this spam" puzzles come down to those two addresses disagreeing.

Replies follow a simple scheme. A 2xx code means done, 3xx means go on (the 354 after DATA), 4xx is a temporary failure that the sender queues and retries later, and 5xx is permanent, so the sender bounces the message. Enhanced codes such as 5.7.1 (RFC 3463) add detail: the middle digit 1 points at addressing problems, and 7 at security or policy, which is where refused relaying and failed authentication land.

Why port 25 and port 587 differ

Port 25 is for servers delivering to other servers. It accepts mail for the domains it hosts without a login, and must refuse everything else, or it becomes an open relay. Port 587, and 465 with implicit TLS, are for your own users and applications: a login is required, and after it the server relays to any destination.

Because infected machines once sent spam straight to port 25, many networks block outbound port 25 by default. On CloudSat, port 25 may be restricted on new accounts and is lifted on request; the submission ports are not affected, as Send email from your server explains.

IMAP: the mailbox lives on the server

IMAP keeps messages and folders on the server and lets every device work on the same copy. Flags such as \Seen, \Flagged and \Deleted sync everywhere, searches run on the server, and the IDLE extension (RFC 2177) lets a client hold a connection open and hear about new mail at once instead of polling. The cost is server storage and indexing, which grows with every mailbox. The current version is IMAP4rev2 (RFC 9051). Servers list the versions and extensions they support in their CAPABILITY reply, and clients use what both sides have.

POP3: download, then usually delete

POP3 does three things: list messages, retrieve them, delete them. It has no folders and no shared read state, so two devices using POP3 on one mailbox each see their own version of it. It still has honest uses: a ticketing system that pulls a dedicated support mailbox, a single machine that archives everything locally, or emptying a mailbox that sits near its quota. For people reading mail on more than one device, IMAP is the right choice.

JMAP: mail over HTTPS and JSON

JMAP replaces IMAP's long-lived connections and line-based commands with JSON requests over HTTPS. Several operations travel in one request, and push notifications are part of the design, which suits mobile and web clients. Server support includes Stalwart, Cyrus IMAP and Apache James; the client list is still short, with aerc, meli and Ltt.rs among the open-source ones. If your server already speaks it, JMAP is worth enabling for clients that use it, while IMAP stays the compatibility default.

Test each trip from a terminal

Work from the outside in: reachability, then TLS, then login, then delivery.

# Is the port reachable at all?
nc -vz -w 5 mail.example.com 587

# Submission with STARTTLS; then type: EHLO test
openssl s_client -connect mail.example.com:587 -starttls smtp -crlf -quiet

# Submission with implicit TLS
openssl s_client -connect mail.example.com:465 -crlf -quiet

# IMAP over implicit TLS; then type: a1 CAPABILITY
openssl s_client -connect mail.example.com:993 -crlf -quiet

# Certificate name and expiry dates on any TLS port
openssl s_client -connect mail.example.com:993 -servername mail.example.com </dev/null 2>/dev/null \
  | openssl x509 -noout -subject -dates

curl speaks all three protocols, which makes it handy for a full round trip. Without a password after the user name, it prompts for one, so the password stays out of your shell history:

# Send a test message through submission; --ssl-reqd refuses to continue without TLS
curl --ssl-reqd --url 'smtp://mail.example.com:587' \
  --user 'alice@example.com' \
  --mail-from 'alice@example.com' --mail-rcpt 'bob@example.org' \
  --upload-file message.txt

# List IMAP folders, then search the inbox for unseen messages
curl --user 'alice@example.com' 'imaps://mail.example.com/'
curl --user 'alice@example.com' 'imaps://mail.example.com/INBOX?UNSEEN'

# List messages over POP3, then fetch the first one
curl --user 'alice@example.com' 'pop3s://mail.example.com/'
curl --user 'alice@example.com' 'pop3s://mail.example.com/1'

The message file needs headers, a blank line and a body:

From: Alice <alice@example.com>
To: Bob <bob@example.org>
Subject: Submission test

Sent with curl over port 587.

Read the failures by stage. A timeout from nc is usually a network block, at a firewall or at the provider. A TLS error points at the certificate or an old protocol version. A 535 reply means the login was refused. A 5.7.1 reply after RCPT TO usually means the server will not relay for you, or does not know the recipient.

Ports to open on a mail server

Port Who connects Open it?
25 Other mail servers Yes, if the server receives mail
587 Your users and applications Yes
465 Users and apps that prefer implicit TLS Optional
993 IMAP clients Yes
995 POP3 clients Only if someone needs POP3
143, 110 IMAP and POP3 starting in plaintext Better closed

Close the plaintext-first ports unless an old device needs them, and then require STARTTLS there. Inbound filtering is a separate layer, covered in our guide to spam and malware filtering on your own mail server.

FAQ

Should an application use port 465 or 587?

Either is encrypted when the client requires TLS. RFC 8314 prefers implicit TLS on 465, but use what your provider documents: Microsoft 365, for example, documents 587 with STARTTLS for authenticated submission, as shown in our guide to sending app email through Microsoft 365.

Is POP3 still worth enabling?

Only for a specific job, such as a system that fetches a dedicated mailbox. For people, IMAP keeps every device in sync, and turning POP3 off removes two ports and one more login path to defend.

Does STARTTLS protect mail between servers?

Only opportunistically: by default a sending server falls back to plaintext if TLS fails. MTA-STS (RFC 8461) and DANE (RFC 7672) let a receiving domain tell senders to require TLS, and TLS reporting (RFC 8460) tells you when delivery to your domain fails that check.

Why does my server reach port 587 but not port 25?

Outbound port 25 is blocked somewhere between you and the destination, usually by the network or hosting provider to stop spam. Ask the provider to lift it for a real mail server, or send through a relay on port 587, which is not blocked.

Run it on CloudSat. Cloud VPS on AMD EPYC and NVMe from $1.98/mo in 13 regions, delivered in under 5 minutes and paid in crypto. See plans or deploy a server.