SMTPServer is an event emitter. Use server.on(), server.once(), and server.off() to manage listeners.
listening
Emitted when the server has bound to a port and is ready to accept connections.The
listening event fires after server.listen() successfully binds to the port.close
Emitted when the server has fully shut down — all connections have closed and the port has been released.This event only fires after all active connections have finished or been forcibly terminated.
error
Emitted on server-level errors (e.g. port already in use, TLS failures). Always attach an error listener to prevent unhandled rejection crashes.Error Object
The error object may include:string
Error message.
number | undefined
SMTP status code (e.g.
421, 451, 550).string | undefined
Error code string (e.g.
"EADDRINUSE", "ECONNRESET").Record<string, unknown> | undefined
Additional context about the error.
Example
connect
Emitted when a new client connection is accepted (afteronConnect succeeds).
ConnectionInfo
string
Unique connection identifier.
string
Server IP address.
number
Server port.
string
Client IP address.
number
Client port.
string
Reverse-DNS hostname of the client.
string
Hostname from HELO/EHLO (populated after the opening command).
Example
Complete Example
Event Method Reference
See the SMTPServer reference for details onon(), once(), and off() methods.