Skip to main content
This example shows how to configure TLS for your SMTP server, supporting both STARTTLS (opportunistic TLS) and implicit TLS.

STARTTLS (port 587)

STARTTLS allows clients to upgrade a plain TCP connection to TLS:
starttls-server.ts
needsUpgrade: true means the server will reject AUTH and MAIL commands until the client runs STARTTLS.

Implicit TLS (port 465)

Implicit TLS starts encryption immediately upon connection:
implicit-tls-server.ts

Generating self-signed certificates

For development, generate a self-signed certificate:
Never use self-signed certificates in production. Use certificates from a trusted CA like Let’s Encrypt.

Using Let’s Encrypt certificates

Load production certificates from disk:

Hot-reloading certificates

Rotate certificates without restarting:
New connections will use the updated certificates immediately. Existing connections continue with the old certificate until they close.

Inspecting the TLS connection

Use the onSecure callback to inspect TLS details:

Client certificate validation

Require clients to present a valid certificate:

SNI (Server Name Indication)

Serve different certificates for different domains:

TLS options reference

string | Buffer
required
Private key in PEM format
string | Buffer
required
Certificate in PEM format
string | Buffer | Array<string | Buffer>
Certificate authority bundle for client cert verification
boolean
default:"false"
Start in implicit TLS mode (true) or allow STARTTLS (false)
boolean
default:"false"
Require STARTTLS before AUTH and MAIL commands
boolean
default:"false"
Request a client certificate during TLS handshake
boolean
default:"false"
Reject clients with invalid or unverifiable certificates
string
Minimum TLS version (e.g., "TLSv1.2")
string
Maximum TLS version (e.g., "TLSv1.3")
Record<string, TLSOptions>
Per-hostname TLS configuration for SNI

Testing TLS connections

You’ll see:

Next steps

TLS guide

Learn more about TLS configuration

Authentication

Add authentication to your server

Configuration reference

Explore all TLS options

Callbacks reference

Learn about onSecure callback