Skip to content

SMTP Server

This SMTP server can be used to receive emails.

Setup

To set up a new SMTP server you can use the smtpServer function.

val smtpServer = smtpServer(port = 25) {
    // access the SMTP server builder in here (more information below)
}

Now you can start the SMTPServer

// this function does not block,
// but it keeps the current thread alive until you call stop()
smtpServer.start()

It is good practice to stop the SMTPServer

smtpServer.stop()


The following code samples all are inside an SMTP server builder.

Optional Configuration

Configuration variables

Inside the SMTP server builder, you have access to the following configuration variables:

Option Description
maxRecipients The maximum amount of recipients the server accepts per message.
default = 1000
maxConnections The maximum amount of connections the server allows at once.
default = 1000
prefferedMaxMessageSize The maximum size of a message. This won't be enforced, this is just an information for the connected client.
default = null (no limit)
connectionTimeout The timeout for waiting for data on a connection.
default = 1 to TimeUnit.MINUTES (1 minute)

The best way to set this is the following
connectionTimeout = 2 to TimeUnit.MINUTES

TLS (Secure connections)

Go to the dedicated TLS page for more details.

Listeners

With listeners, you can receive and process emails.

Commands

Mail (easiest)

Listen to the DATA command (called last, therefore has the most information):

mailListener {
    // get envelope data
    it.envelopeFrom
    it.recipients

    // get the email
    it.email

    // optional response
    it.respondText("OK message received")

    // if the client sent too much data
    it.tooMuchData()
}

Earlier available commands are:

From

Listen to the MAIL FROM command (called first, can only be called once):

fromListener {
    // get the envelope from
    it.envelopeFrom
}

Recipients

Listen to the RCPT TO command (can be called multiple times):

recipientListener {
    // current envelope data
    it.envelopeFrom
    it.currentRecipients // all recipients known so far (including the one responsible for this call)

    // get the recipient responsible for this call
    it.recipient
}

Reject Connections

You can reject connections after receiving any command. Both functions have parameters for a custom reponse and status code.

// reject the connection
it.reject()
// drop the connection
it.dropConnection()

MessageContext

The context gives you more information about the current connection.

// get the MessageContext
it.context
// get the session
it.context.session
// example usage of session
it.context.session?.socket is SSLSocket