New guide: how to warm up a dedicated IP without hurting your reputation. Read now →
Log in
MigoSMTP
SMTP and APIUpdated 26 Oct 2026

SMTP AUTH Mechanisms: PLAIN, LOGIN, CRAM-MD5, SCRAM and XOAUTH2 Compared

How SMTP authentication works and how the main mechanisms differ: PLAIN, LOGIN, CRAM-MD5, SCRAM and OAuth-based XOAUTH2. Security properties, why TLS is required, client and server configuration and troubleshooting.

DW
Written by Daniel Whitfield
IP Reputation and Warm-up Lead
Reviewed by Jonas Lindqvist
  • Published2 Dec 2024
  • Read time7 min
  • Length1,334 words
Share this guide

When an application sends mail through a relay on the submission port, it must prove who it is. SMTP does this with the AUTH command from RFC 4954, which supports several mechanisms from the SASL family. They look similar in a client settings screen ("authentication: normal password", "login", "OAuth2") but behave very differently in security and compatibility. Choosing the wrong one produces mysterious 535 errors, sends credentials in a form that is easy to capture or locks you out when a provider retires basic authentication. This guide explains how AUTH works on the wire, compares the common mechanisms, shows what each means for your configuration and gives a troubleshooting routine. It builds on an SMTP conversation step by step, OAuth2 for SMTP and 535 authentication failures.

Rule 1Always authenticate inside TLS; PLAIN and LOGIN are only as safe as the encryption around them
Rule 2Prefer token-based mechanisms (OAuth) or per-application credentials over shared account passwords
Rule 3Use the mechanism the server advertises in EHLO and that your client library supports
Rule 4Rotate and scope credentials, and watch for repeated failures

How AUTH works

After EHLO (and STARTTLS where required), the server lists its supported mechanisms on a line starting with AUTH. The client picks one, sends AUTH followed by the mechanism name and optional initial data, and the two sides may exchange challenge and response lines (code 334) until the server answers 235 for success or 535 for failure. All data in these exchanges is Base64-encoded so that it can pass through a text protocol; Base64 is an encoding, not encryption.

S: 250-AUTH PLAIN LOGIN XOAUTH2
C: AUTH PLAIN AGFwcC11c2VyAHMzY3JldA==        (Base64 of: \0app-user\0s3cret)
S: 235 2.7.0 Authentication successful

C: AUTH LOGIN
S: 334 VXNlcm5hbWU6                           (Base64 of "Username:")
C: YXBwLXVzZXI=                               (Base64 of the user name)
S: 334 UGFzc3dvcmQ6                           (Base64 of "Password:")
C: czNjcmV0                                   (Base64 of the password)
S: 235 2.7.0 Authentication successful

Anyone who can read this traffic can decode it in a second. That is why servers should refuse AUTH PLAIN and LOGIN until the connection is encrypted, and why a response such as 530 5.7.0 Must issue a STARTTLS command first means "upgrade the connection before sending credentials". See 530 errors.

The mechanisms

MechanismHow it worksSecurity notesTypical use
PLAINSends authorization identity, user name and password in one Base64 string (RFC 4616)Password travels to the server in clear inside the TLS tunnel; simple and widely supportedThe default for most modern submission servers over TLS
LOGINTwo prompts, user name then password, each Base64-encoded (a non-standard but ubiquitous legacy mechanism)Same exposure as PLAIN; interactive prompts make it slightly more error-proneCompatibility with older clients such as some Outlook versions
CRAM-MD5Server sends a challenge; the client replies with a keyed MD5 hash (HMAC) of it using the password (RFC 2195)Password is not sent, but the server must store it in a recoverable form; MD5 is considered weak and the exchange is open to offline guessingLegacy systems; rarely recommended today
DIGEST-MD5Challenge-response with realm and nonceDeprecated by the IETF; poor supportAvoid
SCRAM-SHA-256 (and -1)Salted challenge-response; the server stores a verifier instead of the password (RFC 5802, 7677)Much stronger: no clear password on the wire, resistant to replay, server compromise does not reveal the password directlyModern servers and libraries that support SASL SCRAM
XOAUTH2 / OAUTHBEARERThe client sends a short-lived access token instead of a password (RFC 7628 for OAUTHBEARER)No long-lived password; scopes and revocation; requires an identity provider and token refresh logicGmail, Microsoft 365 and other providers that retire password logins. See OAuth2
EXTERNALIdentity from the TLS client certificate or the connection itselfStrong when certificates are managed wellServer-to-server trust, internal relays
ANONYMOUSNo credentialsNot for submissionRare

Choosing

  1. Look at what the server advertises. Run openssl s_client -starttls smtp -connect host:587, send EHLO and read the AUTH line. See testing SMTP connections.
  2. If the provider supports OAuth2, use it for interactive and workforce accounts. Tokens expire, can be revoked centrally and need not be stored as passwords.
  3. For application and service sending, use a dedicated credential per application (an API key as SMTP password, or an app-specific password), restricted by scope and source where the provider allows it. See API key security.
  4. Use PLAIN or LOGIN only over TLS. If the server offers SCRAM and your library supports it, prefer it.
  5. Avoid CRAM-MD5 and DIGEST-MD5 for new systems unless a legacy constraint forces them.
  6. Do not disable certificate validation to make a connection work; a client that accepts any certificate gives credentials to whoever intercepts it. See certificates.

Ports and encryption modes

PortModeAUTH behaviour
587Submission with STARTTLSConnect in plain text, upgrade, repeat EHLO, then AUTH
465Implicit TLS (SMTPS)TLS from the first byte; AUTH is offered immediately after EHLO
25Server-to-server transferAUTH is usually disabled or restricted; many networks block outbound 25. See ports
2525 and othersAlternative submission ports offered by some providersSame as 587 for most; check provider documentation

Server-side settings

# Postfix with Dovecot SASL, allowing AUTH only after TLS
smtpd_sasl_auth_enable = yes
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_tls_auth_only = yes
smtpd_sasl_security_options = noanonymous
smtpd_sasl_tls_security_options = noanonymous
smtpd_relay_restrictions = permit_sasl_authenticated, reject_unauth_destination

# Dovecot: allow only secure mechanisms to be offered
auth_mechanisms = plain login
disable_plaintext_auth = yes

Server settings should also limit failed attempts per IP and per account, log failures and avoid revealing whether the user name or the password was wrong. See brute-force defence and tarpitting.

Troubleshooting authentication errors

SymptomLikely causeCheck
535 5.7.8 Authentication credentials invalidWrong user name or password, expired app password, wrong realmTest the same credentials with swaks; confirm user name format (full email address or short name)
530 5.7.0 Authentication requiredSending without AUTH, or before STARTTLSOrder of commands in the transcript
504 5.5.4 Unrecognized authentication typeClient requested a mechanism the server does not supportCompare the server's AUTH list with the client setting
454 4.7.0 Temporary authentication failureBackend directory or password database unavailableServer logs, directory health
534 5.7.9 Application-specific password requiredProvider enforces app passwords or OAuth for this accountProvider security settings. See Gmail SMTP settings
Works in one client, fails in anotherDifferent mechanism or encryption mode negotiatedEnable debug logging and compare transcripts
Password with special characters failsEncoding or escaping in configuration files or URLsTest with a simple password; quote or percent-encode correctly
Authentication works but relay is deniedAuthenticated, but sender or recipient policy refusesRead the RCPT TO reply. See open relay checks

Client library notes

Most libraries choose a mechanism automatically from the server's list, usually preferring stronger ones, but they differ in what they support. Some older libraries support only PLAIN and LOGIN; some require an explicit setting for XOAUTH2 and a callback that supplies a fresh access token when the old one expires. If a connection works with one library and fails with another against the same server, compare the mechanism each actually used by enabling the debug output. Another frequent trap is the user name: servers differ on whether they expect the full email address, a short login name or a name with a realm, and the authorization identity in PLAIN can be left empty in nearly all cases. Finally, treat the password field as opaque: a password containing characters such as @, : or % may need escaping when it is embedded in a connection URL or a configuration file, but never when it is passed as a separate parameter.

Security practices

  • Never put credentials in code repositories; use secrets management and rotate regularly.
  • Give each application its own credential, so a leak is traceable and revocable.
  • Restrict credentials to the minimum rights, such as send-only, and to known source IPs when possible.
  • Alert on logins from new countries or unexpected networks.
  • Retire shared mailbox passwords for applications in favour of tokens or API keys.
  • Log successful and failed authentication events, and review them.

Authentication is the gate to your sending reputation. Choose a mechanism that the server really offers, wrap it in TLS, move toward tokens and per-application credentials and keep an eye on failures. The result is fewer 535 surprises and fewer ways for an attacker to turn your relay into a spam cannon.

Send through an authenticated relay Per-application credentials and TLS on standard ports.

Explore SMTP relay

References and further reading

Frequently asked questions

Which SMTP AUTH mechanism is best?

Token-based OAuth where supported, SCRAM where available, otherwise PLAIN or LOGIN strictly over TLS with per-application credentials.

Is Base64 in AUTH PLAIN a form of encryption?

No. Base64 is only an encoding and anyone who captures it can decode the password, so TLS is essential.

What does error 504 Unrecognized authentication type mean?

The client requested a mechanism that the server does not offer; check the AUTH line in the EHLO response.

Why was CRAM-MD5 discouraged?

It relies on MD5, needs the server to store recoverable secrets and offers weaker protection than SCRAM.

Should I use my mailbox password for applications?

No. Use dedicated app passwords, API keys or OAuth tokens that can be scoped and revoked independently.

Topics#smtp auth#sasl#plain#login#oauth
Was this guide helpful?Your answer helps us improve our documentation.

Start sending in minutes.

Create an account, add your domain and send your first email.

Get started Talk to sales