Overview
bun-smtp supports two TLS modes for securing SMTP connections:- Implicit TLS — TLS encryption from the first byte (port 465)
- STARTTLS — Plain connection upgraded to TLS on demand (ports 25, 587)
Implicit TLS
Implicit TLS (also called SMTPS) establishes an encrypted connection immediately. This is typically used on port 465.1
Set secure: true
Enable implicit TLS mode:
2
Provide certificates
Add your TLS certificate and private key:
3
Listen on port 465
Start the server on the standard SMTPS port:
STARTTLS
STARTTLS allows clients to upgrade a plain connection to TLS. This is advertised in the EHLO response and is the standard for ports 25 and 587.When
key and cert are provided but secure is not set (or is false), STARTTLS is automatically advertised in the EHLO capabilities.Hiding STARTTLS
To support STARTTLS without advertising it in EHLO (clients can still use it if they know about it):Requiring STARTTLS
Force clients to complete STARTTLS before sending AUTH or MAIL commands:Development Mode (No Certificate)
When nokey or cert is provided, bun-smtp uses a built-in self-signed certificate. This lets you test TLS functionality without certificate setup:
View the built-in certificate details
View the built-in certificate details
The built-in certificate is a self-signed localhost certificate from the original smtp-server package:Subject:
src/smtp-server.ts
CN=localhost
Valid: 2015-02-12 to 2025-02-09SNI (Server Name Indication)
Serve different certificates for different hostnames using SNI:sniOptions accepts either a plain object or a Map<string, TLSOptions> for dynamic certificate management.Dynamic SNI with Map
Use aMap for runtime certificate updates:
Validating the TLS Handshake
UseonSecure to inspect or reject connections after TLS is established:
onSecure is called after both implicit TLS and STARTTLS upgrades. The socket parameter is a Bun Socket, not a Node.js tls.TLSSocket.TLS Options Reference
Client Certificate Authentication
Require and validate client certificates:1
Enable client certificates
2
Validate in onSecure
Updating Certificates at Runtime
Rotate certificates without restarting the server:New connections will use the updated certificates immediately. Existing connections continue using the old certificates until they close.
Port Recommendations
Port 25
MTA-to-MTATraditional SMTP port for server-to-server communication. Usually supports STARTTLS but doesn’t require it.
Port 587
Message SubmissionStandard port for client-to-server communication. Should require STARTTLS and authentication.
Port 465
SMTPSImplicit TLS from connection start. Use
secure: true for this port.Complete Examples
- Production (Port 587)
- SMTPS (Port 465)
- Development