Keycloak with SendGrid: SMTP Setup That Actually Delivers

Mariam SolimanMariam Soliman8 min read

To send Keycloak email through SendGrid, open Realm settings, then the Email tab, and set the host to smtp.sendgrid.net, the port to 587 with StartTLS on, the username to apikey, and the password to a SendGrid API key that has Mail Send permission. The From address must be a verified SendGrid sender or sit on a domain you have authenticated in SendGrid, otherwise SendGrid rejects the message.

This guide is for you if your realm already works and you have a SendGrid account, but verification and password reset emails fail the test connection, get rejected, or end up in spam. It covers SendGrid only, on Keycloak 26.x, and walks through the exact Email tab values, the API key, domain authentication, port choice and the errors you are most likely to see. For other providers, see our general Keycloak email configuration guide, and see also the email not sending troubleshooting guide.

What do you need before you start?

You need three things in place before you touch the Email tab:

  • Admin access to the realm in the Keycloak admin console, with an email address set on your admin user.
  • A SendGrid API key with Mail Send permission, which we create in the next section.
  • A sender identity in SendGrid for the address you want in the From field: either a single verified sender or an authenticated domain. SendGrid states that it “requires verified Sender Identities” and that a sender identity represents your From address.

See also 8 default configurations to adjust right away on your Keycloak cluster.

How do you create a SendGrid API key that can only send mail?

In the SendGrid console, go to Settings, then API Keys, and create a new key. SendGrid offers Full Access, Custom Access, Billing Access and Email Address Validation Access as permission levels. Choose Custom Access and give the key the Mail Send permission and nothing else. Keycloak only ever sends mail through this key, so there is no reason for it to be able to read your contacts, change settings or create other keys if it leaks.

SendGrid displays the API key only once at creation, so copy it straight into the Keycloak password field or into your secret store. If you lose it, delete it and create a new one, since SendGrid cannot show it to you again. Use one key per environment (staging and production, for example) so you can rotate or revoke one without breaking the other.

Why is the SMTP username literally apikey?

SendGrid’s SMTP documentation says the username “is the exact string “apikey” and not the API key itself”, and the password is the API key you generated. So the username field carries the same fixed word apikey for every account: not your SendGrid login email, and not the key.

Which values go in Keycloak’s Email tab?

Open the realm, go to Realm settings, then the Email tab. The Keycloak Server Administration Guide lists the fields in two groups, Template and Connection & Authentication. These are the values for SendGrid:

Field Value for SendGrid
From An address on your authenticated domain, or your single verified sender, for example no-reply@example.com
From display name The name users should see, such as your product name
Reply to Optional: a monitored address if users reply to these emails
Host smtp.sendgrid.net
Port 587
Encryption StartTLS on, SSL off
Authentication On
Username apikey
Authentication type Password
Password Your Mail Send API key

SendGrid also recommends using the host name smtp.sendgrid.net rather than pointing at SendGrid IP addresses, so do not hard-code an IP in the Host field or in a firewall rule that you will forget about later.

Save the tab, then click Test connection and check the result.

How Keycloak sends a message through SendGrid: connect on 587, upgrade with STARTTLS, authenticate with apikey, then the From address is checked

Keycloak connects to smtp.sendgrid.net on port 587 (1), SendGrid offers STARTTLS (2), Keycloak upgrades the connection to TLS (3), authenticates with the username apikey and the API key (4), and sends the realm’s From address, which SendGrid checks against a verified sender identity (5) before it accepts the message (6).

Which port should you use: 587, 465 or 2525?

SendGrid accepts SMTP on ports 25, 2525 and 587 for unencrypted or TLS connections, and on 465 for SSL, and it notes that TLS on port 587 is typically recommended. In Keycloak terms that gives you three sensible choices:

  • 587 with StartTLS. The default choice. StartTLS uses the STARTTLS command defined in RFC 3207, an SMTP extension that lets the client and server switch the connection to TLS.
  • 465 with SSL. SendGrid lists 465 for SSL connections, and the Keycloak guide calls 465 “the default port for SSL/TLS”. Use it if you prefer no STARTTLS step, and remember to turn StartTLS off.
  • 2525 with StartTLS. The same as 587 on a different port, an alternative to try if 587 does not connect from your network. Wherever Keycloak runs, the egress rule has to allow whichever port you pick (see also the Docker Compose production setup).

SendGrid also accepts port 25, but this guide sticks to the three choices above (for connection problems, see also the troubleshooting guide). Whatever you choose, tick the encryption checkbox that goes with the port in SendGrid’s list: StartTLS for 587 and 2525, SSL for 465.

The cluster, without the on-call

Skycloak runs real upstream Keycloak in the region you choose, and takes the upgrades, backups, certificate rotation and patching off your team. No fork, so you can export and self-host whenever you want.

What changed for StartTLS in Keycloak 26.8.1?

Our CVE-2026-107121 advisory describes a change in Keycloak 26.8.1 that the upgrading guide lists as a breaking change: with StartTLS enabled, earlier versions carried on in plaintext when the server did not offer the upgrade, and 26.8.1 makes the upgrade mandatory, so the email is not sent instead. RFC 3207 describes the risk of the old behavior: “A man-in-the-middle attack can be launched by deleting the “250 STARTTLS” response from the server”.

SendGrid supports STARTTLS on 587 and 2525, so a correctly configured realm keeps working after the upgrade. Because your SendGrid API key crosses the public internet on every connection, run a Keycloak version that includes the fix (the advisory names 26.8.1), and if you were on an older version on a network you do not trust, rotating the key is a reasonable precaution.

Single sender verification or domain authentication?

SendGrid lets you verify a sender identity using either domain authentication or single sender verification. Single sender verification confirms one email address: SendGrid sends a verification email to that address and you click the link in it. It is quick, but SendGrid’s own page says it “Works for testing only”, and you have to verify every address separately.

Domain authentication proves that you control the whole domain, so you can send from any address on it. SendGrid recommends it and says to “configure domain authentication before launching your mail send into production”, and it notes that an authenticated domain improves deliverability and sender reputation. That is the part that keeps password reset emails out of spam, because receiving mail servers can check that SendGrid is allowed to send for your domain.

The domain authentication setup works through DNS records that you add at your DNS provider:

  • SPF (Sender Policy Framework) lists which servers may send mail for your domain.
  • DKIM (DomainKeys Identified Mail) publishes the public key that receiving servers use to check the signature SendGrid adds to each message.
  • DMARC tells receiving servers what to do with messages that fail those checks. SendGrid’s example record is v=DMARC1; p=none;, and its DMARC page says that with none the receiving server delivers the message, and that none is the setting for collecting DMARC reports.

With automated security on, which is the default, SendGrid gives you CNAME records to add and then maintains the SPF and DKIM records behind them for you. SendGrid says DNS verification can take up to 48 hours after you add the records. Note that a subdomain cannot inherit an authenticated root domain, according to the sender identity page, so if your From address is no-reply@auth.example.com, authenticate auth.example.com itself.

Troubleshooting: Keycloak test connection errors with SendGrid

When Test connection fails, the error usually contains the SMTP reply SendGrid sent. This table maps the common ones to their cause and fix.

What you see Likely cause Fix
Authentication fails Username is your login email or the key itself, or the key was deleted or lacks Mail Send Set the username to apikey and paste a current key with Mail Send permission (API keys)
550 Unauthenticated Senders Not Allowed Keycloak sent the message without logging in Turn Authentication on and fill in the username and password (SendGrid SMTP errors)
You are not authorized to send from that email address The From address does not match a verified sender identity Verify the address, or authenticate its domain, then retest (SendGrid SMTP errors)
Connection times out Outbound port blocked by a firewall, security group or network policy Allow egress on the port, or switch to 2525 with StartTLS
TLS or STARTTLS error The port and the encryption option may not match (CVE-2026-107121 advisory) Use 587 or 2525 with StartTLS, or 465 with SSL, as in SendGrid’s port list
Mail stops sending after upgrading to 26.8.1 The relay does not support STARTTLS on that port (CVE-2026-107121 advisory) Check the host is smtp.sendgrid.net and the port is 587 or 2525 (upgrading guide)
Test passes but users find mail in spam Sending from a single verified sender with no domain authentication Authenticate the domain so SPF and DKIM pass

If the test succeeds but real messages are delayed, SendGrid’s errors page explains that a 421 deferral is usually the receiving server’s policy and that SendGrid retries for up to 72 hours.

FAQ

What is the SendGrid SMTP username for Keycloak?

The username is the literal string apikey for every SendGrid account. The password is your SendGrid API key, and it needs at least the Mail Send permission.

Can Keycloak use the SendGrid Web API instead of SMTP?

This guide uses SMTP, because the Keycloak guide says that “To enable Keycloak to send emails, you provide Keycloak with your SMTP server settings.” SendGrid’s SMTP relay is what the realm’s Email tab connects to.

Why does my Keycloak password reset email from SendGrid land in spam?

Usually the domain in your From address is not authenticated in SendGrid, so receiving servers cannot confirm that SendGrid may send for it. Set up domain authentication with the SPF and DKIM records SendGrid gives you and wait for DNS to verify.

Do I need a separate SendGrid key per Keycloak realm?

You do not have to. The Keycloak guide sets up email under each realm’s Realm settings, and any key with Mail Send permission works there. A separate key per environment, or per realm if different teams own them, makes it easier to rotate one without touching the rest.

Next steps

Enter the SendGrid values in your realm’s Email tab, click Test connection, and fix any error with the table above. Then authenticate your sending domain in SendGrid so that real verification and password reset emails reach the inbox, and run a password reset end to end to confirm it. If you would rather not run the Keycloak cluster underneath it yourself, you can try Skycloak’s managed Keycloak hosting.

The cluster, without the on-call

Skycloak runs real upstream Keycloak in the region you choose, and takes the upgrades, backups, certificate rotation and patching off your team. No fork, so you can export and self-host whenever you want.

Mariam Soliman
Written by
Customer Success, Skycloak

Mariam works in customer success at Skycloak, helping teams get set up on managed identity and stay successful once they are live. She writes about the questions customers bring to us most often.

© 2026 Skycloak. All Rights Reserved. Design by Yasser SolimanKeycloak™ is a trademark of the Linux Foundation. Skycloak is not affiliated with the Keycloak project.