SMTP authentication, defined by the AUTH extension in RFC 4954, lets a client prove who it is before the server accepts mail for relay. The server lists the mechanisms it accepts in its EHLO response, and the client picks one. For most applications the right choice is PLAIN or LOGIN over a TLS connection, or OAuth 2.0 when the mail provider requires tokens instead of passwords.
The mechanism matters less than the channel. A password sent with PLAIN inside TLS is safe from eavesdroppers, while a stronger mechanism over an unencrypted connection still leaks the message and metadata. This page explains each mechanism with the real exchange, then how to pick one and fix the errors that follow.
How SMTP authentication works
The client connects, sends EHLO, upgrades with STARTTLS (or connects with implicit TLS on port 465) and sends EHLO again. The server now advertises a line such as 250-AUTH PLAIN LOGIN XOAUTH2. The client issues AUTH followed by a mechanism name and, where the mechanism allows, an initial response. Binary data in this exchange is base64 encoded. A 334 reply carries a challenge, 235 means success, and 535 means the credentials were rejected.
S: 250-AUTH PLAIN LOGIN XOAUTH2
C: AUTH PLAIN AHVzZXJAZXhhbXBsZS5jb20AczNjcmV0
S: 235 2.7.0 Authentication successfulServers commonly refuse AUTH before TLS with 530 5.7.0 and a message such as must issue a STARTTLS command first. Submission servers are configured this way on purpose. See 530 must issue STARTTLS first and SMTP submission.
PLAIN
PLAIN (RFC 4616) sends an optional authorisation identity, the username and the password in one message, separated by NUL bytes and base64 encoded: authzid NUL authcid NUL password. It is simple, supported almost everywhere and works with any password store on the server, including hashed passwords. Its weakness is that the password is sent in a form anyone can decode, so it is only acceptable inside TLS.
# Build the PLAIN initial response (empty authzid)
printf '\[email protected]\0s3cret' | base64
# AHVzZXJAZXhhbXBsZS5jb20AczNjcmV0LOGIN
LOGIN is an older, non-standard mechanism that asks for the username and password in two separate steps, each base64 encoded. Many clients still implement it, and some servers only advertise it, which is why you will see both names in one AUTH line. It has the same security properties as PLAIN.
C: AUTH LOGIN
S: 334 VXNlcm5hbWU6
C: dXNlckBleGFtcGxlLmNvbQ==
S: 334 UGFzc3dvcmQ6
C: czNjcmV0
S: 235 2.7.0 Authentication successfulThe two 334 prompts decode to Username: and Password:. Base64 is an encoding, not encryption.
CRAM-MD5
CRAM-MD5 (RFC 2195) is a challenge and response scheme. The server sends a random challenge, and the client replies with the username and an HMAC-MD5 digest of the challenge keyed with the password. The password never crosses the network, which was a real benefit before TLS was universal. The costs are that the server must keep the password, or an equivalent secret, in a form it can compute with, so it cannot store a salted slow hash; MD5 is no longer considered strong; and it offers no protection for the message itself. Many modern providers have dropped it. If TLS is available, prefer PLAIN inside TLS. Where the server stores salted hashes and supports the SCRAM family (RFC 5802), that is a stronger password mechanism, but client support in mail libraries is uneven.
XOAUTH2 and OAUTHBEARER
Large mailbox providers increasingly refuse passwords for SMTP and require an OAuth 2.0 access token instead. The client obtains a short lived access token from the provider's authorisation server through a registered application, then presents it with AUTH XOAUTH2. XOAUTH2 is a vendor mechanism first documented by Google, later adopted by others. OAUTHBEARER (RFC 7628) is the standardised equivalent. The initial response packs the user and the bearer token, separated by control characters (^A is byte 0x01), then base64 encodes the whole string.
# XOAUTH2 initial response before base64 encoding
[email protected]^Aauth=Bearer ya29.EXAMPLE_TOKEN^A^A
C: AUTH XOAUTH2 dXNlcj11c2VyQGV4YW1wbGUuY29tAWF1dGg9QmVhcmVyIHlhMjkuRVhBTVBMRV9UT0tFTgEB
S: 235 2.7.0 AcceptedOn failure the server answers with a 334 containing a base64 JSON error, and the client must send an empty line to receive the final 535. Token handling is the hard part: access tokens expire, usually in under an hour, so the client has to hold a refresh token and renew, and the application needs the correct scope and consent for sending mail. Use a library that implements the flow rather than building it by hand. Provider rules on basic authentication and OAuth change regularly, so confirm the current requirements in the provider's documentation. See SMTP OAuth 2.0 and Microsoft 365 SMTP AUTH disabled.
Comparing the mechanisms
| Mechanism | Password on the wire | Standard | Server storage needs | Use when |
|---|---|---|---|---|
| PLAIN | Yes, base64 | RFC 4616 | Any, including hashes | TLS is in place; the default choice |
| LOGIN | Yes, base64 | Not standardised | Any | Client or server only supports it |
| CRAM-MD5 | No, HMAC of a challenge | RFC 2195 | Usable secret, not a slow hash | Legacy systems only |
| SCRAM-SHA-256 | No | RFC 5802 and RFC 7677 | Salted verifier | Both sides support it |
| XOAUTH2 | No, access token | Vendor specification | No password, token validation | Provider requires OAuth 2.0 |
| OAUTHBEARER | No, access token | RFC 7628 | Token validation | Token auth with a standard mechanism |
Choosing a method in practice
- Connect on port 587 with
STARTTLS, or port 465 with implicit TLS. See which SMTP port to use. - Verify the server certificate in your client. Disabling verification defeats the purpose of TLS.
- Read the
AUTHline the server advertises after TLS and use the strongest mechanism both sides support. - If the provider issues API keys or SMTP passwords, use
PLAINorLOGINwith that credential and keep it in a secret store, not in source code. - If the provider requires OAuth 2.0, use a maintained library to obtain and refresh tokens.
- Give each application its own credential so you can rotate or revoke one without affecting the rest. See credential rotation.
For a deeper list of what servers offer, see SMTP AUTH mechanisms and SMTP AUTH methods.
Security practices that matter more than the mechanism
- Enforce TLS before AUTH on the server. Advertise
AUTHonly after the connection is encrypted, so a client cannot send a password in clear text by mistake. - Limit failed attempts. Rate limit and lock out repeated failures per account and per source address, because submission ports are a constant target for password guessing.
- Use unique, long secrets. Generated credentials of at least 20 random characters are far safer than reused human passwords.
- Scope what each credential can do. A sending credential should not also administer the account.
- Watch for use from unexpected places. A sudden burst from a new address is the usual sign of a leaked credential. See responding to a compromised sending account.
Troubleshooting authentication failures
| Symptom | Likely cause | Fix |
|---|---|---|
| 535 authentication failed | Wrong username or password, or revoked credential | Re-enter credentials and check the username format the provider expects. See 535 fix |
| 530 must issue STARTTLS first | AUTH attempted before TLS | Enable STARTTLS on 587 or implicit TLS on 465 |
| 504 mechanism not supported | Client asked for a mechanism the server does not offer | Use one from the server AUTH line |
| AUTH not advertised | Connected on the wrong port, or TLS not yet negotiated | Run EHLO again after STARTTLS |
| XOAUTH2 returns 535 with JSON | Expired or wrongly scoped token | Refresh the token and check scope and user |
| Works on one network only | Port blocked by a firewall | Test 587 and 465; see SMTP firewalls |
| Account locks after errors | Repeated failed logins from a script | Fix the credential, then see brute force protection |
When to use each, and next steps
Use PLAIN or LOGIN with TLS for service accounts and application credentials, XOAUTH2 or OAUTHBEARER where a provider mandates tokens for user mailboxes, and avoid CRAM-MD5 for new systems. For applications that send high volumes, an HTTP API with a bearer key avoids most of these choices, as covered in SMTP versus API. To check that your authentication works end to end, follow testing an SMTP server from the command line.
Authenticate with a credential you control Connect to smtp.migosmtp.com on port 587 or 465 over TLS, or call the REST API with a Bearer key.
References and further reading
Frequently asked questions
Which SMTP authentication method should I use?
Use PLAIN or LOGIN over a TLS connection with a credential unique to the application. Use XOAUTH2 or OAUTHBEARER if the provider requires OAuth 2.0 tokens.
Is SMTP AUTH LOGIN secure?
Only inside TLS. The username and password are base64 encoded, which anyone can decode, so the encryption of the connection provides the protection.
Is CRAM-MD5 better than PLAIN?
It avoids sending the password, but it relies on MD5 and needs the server to hold a usable secret. PLAIN inside TLS is generally preferred today.
What is XOAUTH2 for SMTP?
It is an authentication mechanism that sends an OAuth 2.0 access token instead of a password. The token is short lived and must be refreshed through the provider.
Why does SMTP return 530 before authentication?
The server requires TLS before it accepts AUTH. Send STARTTLS on port 587, or use implicit TLS on port 465, then authenticate.