For outgoing mail, use your provider's SMTP host on port 587 with STARTTLS, or port 465 with implicit SSL/TLS if the provider offers it. Authenticate with your full email address and an app password or OAuth, and set the From address to the account you sign in with. Port 25 is for server-to-server delivery and is blocked by many networks, so avoid it for client submission.
The table below lists the main providers. Hostnames and authentication rules do change, and administrators can disable options on business accounts, so confirm against the provider's current documentation when something fails. The rest of the article explains how to pick between 587 and 465, what authentication to expect and how to read the common errors.
SMTP host and port by provider
| Provider | SMTP host | Ports and security | Authentication notes |
|---|---|---|---|
| Gmail and Google Workspace | smtp.gmail.com | 465 SSL/TLS, 587 STARTTLS | App password with 2-Step Verification, or Google sign-in (OAuth) |
| Outlook.com, Hotmail, Live | smtp-mail.outlook.com | 587 STARTTLS | Modern authentication is replacing passwords for consumer accounts |
| Microsoft 365 | smtp.office365.com | 587 STARTTLS | Authenticated SMTP must be enabled for the mailbox by the administrator |
| Yahoo Mail | smtp.mail.yahoo.com | 465 SSL/TLS, 587 STARTTLS | App password required |
| iCloud Mail | smtp.mail.me.com | 587 STARTTLS | App-specific password, with the full iCloud address as username |
| Zoho Mail | smtp.zoho.com | 465 SSL/TLS, 587 STARTTLS | Regional domains such as smtp.zoho.eu and smtp.zoho.in exist, and paid organisation accounts may use a different host |
| AOL Mail | smtp.aol.com | 465 SSL/TLS, 587 STARTTLS | App password required |
More detail on individual providers: Gmail SMTP settings and Outlook and Microsoft 365.
Choosing between port 587 and port 465
Both ports are for mail submission, meaning a client or application handing a message to its own provider. Port 587 begins as a plain connection and upgrades with the STARTTLS command. Port 465 is encrypted from the first byte. RFC 8314 recommends implicit TLS for submission, and RFC 6409 defines port 587 for message submission, so both are valid.
| Question | 587 with STARTTLS | 465 with implicit TLS |
|---|---|---|
| Encryption starts | After the STARTTLS command | Immediately |
| Client setting name | STARTTLS or TLS if available | SSL/TLS or SSL |
| Risk of mistakes | Downgrade if the client allows plain fallback | Few, since there is no plain phase |
| Support | Almost universal | Wide, but not offered by every provider |
| Firewall behaviour | Usually open on consumer networks | Usually open, sometimes less commonly allowed in strict networks |
If your library offers a choice, require encryption rather than opportunistic encryption. Opportunistic STARTTLS can be stripped by an attacker on the path, while a required setting makes the connection fail instead.
Authentication: what to expect
- Username. Usually the full email address.
- Password. Many providers no longer accept the ordinary account password in third-party clients. Use an app password. See app passwords explained.
- OAuth 2.0. Where available, sign-in happens in the provider's page and the client holds a token. See OAuth 2.0 for SMTP and IMAP.
- From address. Most providers rewrite or reject a From address that does not belong to the signed-in account, unless you have configured an alias.
- Business accounts. Administrators can disable SMTP authentication, require OAuth or restrict who may send. If your settings are correct and still fail, ask your administrator.
Test your settings from the command line
Before changing an application, check that the server accepts your login. This example uses curl against the Gmail server and a throwaway message file. Replace the values, and use an app password.
cat > msg.txt <<'EOF'
From: [email protected]
To: [email protected]
Subject: SMTP test
Test message.
EOF
curl --ssl-reqd --url 'smtps://smtp.gmail.com:465' \
--user '[email protected]:APP_PASSWORD' \
--mail-from '[email protected]' --mail-rcpt '[email protected]' \
--upload-file msg.txtFor STARTTLS on 587, change the URL to smtp://smtp.gmail.com:587 and keep --ssl-reqd. The same pattern works for the other providers by changing the host and credentials. A success returns without errors, and a failure prints the SMTP reply code, which you can look up in an SMTP reply code reference.
Configuration in code
Most libraries need the same few values. This Python example uses implicit TLS and reads the secret from the environment.
import os, smtplib
from email.message import EmailMessage
msg = EmailMessage()
msg['From'] = '[email protected]'
msg['To'] = '[email protected]'
msg['Subject'] = 'SMTP test'
msg.set_content('Test message.')
with smtplib.SMTP_SSL('smtp.example.com', 465, timeout=30) as s:
s.login('[email protected]', os.environ['SMTP_PASSWORD'])
s.send_message(msg)For STARTTLS, use smtplib.SMTP(host, 587), then call starttls() before login().
Common mistakes with hostnames and usernames
- Using the incoming server for sending. An IMAP or POP host such as imap.example.com does not accept submission. SMTP has its own host.
- Using an alias as the username. Many providers authenticate only the primary address. Sign in with the main mailbox and add the alias as an allowed From address.
- Typing the password with auto-correct. Mobile keyboards capitalise or insert spaces into app passwords. Remove the spaces that providers add for readability when they display a code in groups.
- Leaving authentication switched off. A setting such as my outgoing server requires authentication must be ticked in many desktop clients.
- Pointing at a generic hosting server. For a custom domain on shared hosting, the SMTP host is usually given in the hosting control panel, not by Gmail or Outlook.
How custom domains and shared hosting differ
The providers in the table are mailbox services. If your address is on your own domain, the SMTP host depends on where that mailbox lives. If the mailbox is hosted by Google Workspace or Microsoft 365, use those hosts with your domain address as the username. If it lives at a web host, use the hostname and ports shown in the hosting panel, and check whether the host blocks outbound port 25 or limits hourly sending.
A frequent surprise is that mail sent through a web host's SMTP server for a domain whose DNS points elsewhere fails SPF or DKIM checks at the receiver. The sending IP must be authorised by the domain's SPF record, and the DKIM signature must match a published key. Without that, messages are accepted by your server but quarantined by the recipient.
Limits you will meet on personal and business mailboxes
Personal mailbox SMTP is designed for people, not for applications. Providers set daily recipient limits, rate limits and rules about bulk content, and they change these over time. Check the provider's current documentation for exact numbers rather than relying on a figure from memory. If you are sending newsletters, invoices from an application or password resets at volume, a dedicated sending service with its own credentials and event reporting is the more reliable route. See when to switch from Gmail or Outlook to a sending service.
Troubleshooting
| Error or symptom | Likely cause | Fix |
|---|---|---|
| 535 authentication failed | Wrong password, no app password, or basic auth disabled | Create an app password or use OAuth. See SMTP 535 authentication failed |
| 530 must issue STARTTLS first | Client connected on 587 without enabling STARTTLS | Enable STARTTLS or encryption in the client. See 530 5.7.0 fix |
| Connection timed out | Port blocked by firewall or ISP, or wrong host | Try the alternate port, another network or check the hostname |
| Handshake or SSL error | STARTTLS selected for 465, or implicit TLS for 587 | Match the mode to the port |
| Certificate name mismatch | Wrong hostname or a proxy intercepting TLS | Use the provider hostname exactly as documented |
| 553 or 550 sender rejected | From address does not belong to the account | Send from the signed-in address or configure an alias |
| Authentication disabled message on Microsoft 365 | Authenticated SMTP off for the mailbox | Administrator must enable authenticated SMTP (client submission) for the mailbox |
Security checklist and next steps
- Require TLS on every connection and never fall back to plain text.
- Store credentials in environment variables or a secret manager, not in the repository.
- Use a separate app password per application, and revoke it when the application is retired.
- Do not disable certificate validation to make an error go away. Fix the hostname or the trust store.
- Set up SPF, DKIM and DMARC for any domain that sends mail so that receivers can verify it.
Once the connection works, the next question is reputation. Mail that is accepted by your provider can still land in spam if the domain is not authenticated. Read authenticating your sending domain for the records you need.
Need SMTP for an application? Get relay credentials on ports 587 and 465 with delivery, bounce and complaint events.
References and further reading
Frequently asked questions
Should I use port 587 or 465?
Either is valid. 587 with STARTTLS is the most widely supported, and 465 with implicit TLS is simpler and encrypted from the start.
What is the Gmail SMTP server?
smtp.gmail.com, port 465 with SSL/TLS or port 587 with STARTTLS, using an app password or Google sign-in.
Why is port 25 not recommended for mail clients?
It is meant for server-to-server delivery and is often blocked by ISPs and cloud networks for end users.
Do I need an app password for SMTP?
For Gmail, Yahoo, iCloud and AOL, generally yes, unless the client supports OAuth sign-in.
Why does my SMTP login fail when the password is right?
The provider may require an app password or OAuth, or an administrator may have disabled authenticated SMTP.