First, we need to create a Gradle project with some dependencies: (→ Tutorial)

dependencies {
    compile "io.javalin:javalin:3.4.1"
    compile "org.apache.commons:commons-email:1.4"
    compile "org.slf4j:slf4j-simple:1.7.26"

Setting up the backend

We need three endpoints: GET '/', POST '/contact-us' and GET '/contact-us/success':

import io.javalin.Javalin
import org.apache.commons.mail.*

fun main(args: Array<String>) {

    val app = Javalin.create().start(7000)

    app.get("/") { ctx ->
                <form action="/contact-us" method="post">
                    <input name="subject" placeholder="Subject">
                    <textarea name="message" placeholder="Your message ..."></textarea>
    }"/contact-us") { ctx ->
        SimpleEmail().apply {
            setAuthenticator(DefaultAuthenticator("YOUR_EMAIL", "YOUR_PASSWORD"))
        }.send() // will throw email-exception if something is wrong

    app.get("/contact-us/success") { ctx -> ctx.html("Your message was sent") }


In order to get the above code to work, you need to make some changes:

  • Change YOUR_EMAIL to your gmail account ([email protected])
  • Change YOUR_PASSWORD to your gmail password*
  • Change RECEIVING_ADDRESS to where you want the email to be sent

*It might be a good idea to create a test-account instead of using your real gmail credentials.

When you’ve made the changes to the code, run the program and go to http://localhost:7000. You will see a simple unstyled form with an input field, a textarea and a button. Fill in the form and click the button to test your email server. After you click the button, your browser is redirected to /contact-us/success (if the email was sent).

Any emails you have sent will show up in your Sent folder in the gmail web-interface.