Kotlinmailer v1.1.21 Help

Get Started with Kotlinmailer

In this tutorial, you will learn how to send emails using Kotlinmailer and integrate it within your existing projects.

By the end of this tutorial, you will be able to do the following:

  • Configure an SMTP server connection

  • Send emails using Kotlinmailer

Prerequisites

Before starting this tutorial, you should have the following prerequisites:

Create a new project

First, you will need a basic Kotlin project setup to build upon. You can download a preinitialized project or follow the steps below to generate a new project with Gradle.

  1. In a terminal window, navigate to the destination where you want to create your project and run the following commands to create a new folder and change directory into it:

    mkdir kotlinmailer-app cd kotlinmailer-app
  2. Run the gradle init task to initialize a new Gradle project:

    gradle init

    When prompted, select the following options:

    • 1: Application project type.

    • 2: Kotlin implementation language.

    For the other questions, press enter to use the default values. The output will look like the following:

    Select type of build to generate: 1: Application 2: Library 3: Gradle plugin 4: Basic (build structure only) Enter selection (default: Application) [1..4] Select implementation language: 1: Java 2: Kotlin 3: Groovy 4: Scala 5: C++ 6: Swift Enter selection (default: Java) [1..6] 2 Enter target Java version (min: 7, default: 21): Project name (default: exposed-kotlin-app): Select application structure: 1: Single application project 2: Application and library project Enter selection (default: Single application project) [1..2] Select build script DSL: 1: Kotlin 2: Groovy Enter selection (default: Kotlin) [1..2] Select test framework: 1: kotlin.test 2: JUnit Jupiter Enter selection (default: kotlin.test) [1..2] Generate build using new APIs and behavior (some features may change in the next minor release)? (default: no) [yes, no] > Task :init To learn more about Gradle by exploring our Samples at https://docs.gradle.org/8.8/samples/sample_building_kotlin_applications.html BUILD SUCCESSFUL in 28s 1 actionable task: 1 executed
  3. Once the project has been initialized, open the project folder in your IDE. To open the project, use the following command:

    idea .
    code .

Add dependencies

Before you start using Kotlinmailer, you need to provide dependencies to your project.

  1. Navigate to the gradle/libs.versions.toml file and define the Kotlinmailer version and libraries:

    [versions] //... kotlinmailer = "1.1.21" [libraries] //... kotlinmailer-core = { module = "at.quickme.kotlinmailer", name = "core", version.ref = "kotlinmailer" }
    • The core module provides the foundational components and abstractions needed to work with databases in a type-safe manner and includes the DSL API.

    • The html module provides the HTML DSL API to create emails with kotlinx.html.

  2. Navigate to the app/build.gradle.kts file and add the Exposed and H2 database modules into the dependencies block:

    dependencies { //... implementation(libs.kotlinmailer.core) // Optional implementation(libs.kotlinmailer.html) //... }
  3. Last but not least, load the Gradle changes in your IDE:

    In IntelliJ IDEA, click the notification Gradle icon (intelliJ IDEA gradle icon) on the right side of the editor to load Gradle changes.

    In Visual Studio Code, open the terminal and execute:

    ./gradlew

Configure an SMTP Server

For Kotlinmailer to send emails, you need to configure an SMTP server connection.

  1. Navigate to app/src/main/kotlin/org/example/ and open the App.kt file.

  2. Replace the contents of the App.kt file with the following implementation:

    package org.example import at.quickme.kotlinmailer.delivery.mailerBuilder fun main() { val mailer = mailerBuilder( host = "smtp.example.com", port = 25, username = "john.doe@example.com", password = "secure-password" ) }

    The mailerBuilder() function creates an instance of a class that represents the SMTP server connection.

    With this, you've added Kotlinmailer to your Kotlin project and configured a SMTP server connection. You're now ready to send your first email.

Send a simple text email

Let us add the app’s functionality by sending an email.

  1. In the same App.kt file, add the following code to your implementation:

    package org.example import at.quickme.kotlinmailer.delivery.mailerBuilder fun main() { val mailer = mailerBuilder( host = "smtp.example.com", port = 25, username = "john.doe@example.com", password = "secure-password" ) val sender = InternetAddress("John Doe <john.doe@example.com>") emailBuilder { from(sender) to("alice.doe@exmaple.com") withSubject("Let's get married 💍") withPlainText("My dear Alice, I love you! Will you marry me?") }.send(mailer) }

    Here's the breakdown:

    The InternetAddress() function creates an instance of the InternetAddress class, which represents the sender's email identity. The first part `John Doe` is the sender's name, and the second part consists of the sender's email address.

    val sender = InternetAddress("John Doe <john.doe@example.com>")

    The emailBuilder function creates an instance of the Email class, which represents the email to be sent. The emailBuilder function is a DSL function that allows you to build an email using a set of functions. As you can see, we set parameters such as recipient, subject and body text for the email.

    emailBuilder { from(sender) to("alice.doe@exmaple.com") withSubject("Let's get married 💍") withPlainText("My dear Alice, I love you! Will you marry me?") }

    The send() function finalises the process and sends the email using the previously configured SMTP connection.

  2. You should now see in the recipents email inbox that your email with the subject "Let's get married 💍" has been successfully sent.

Next steps

Great job! You've now implemented a simple console application that uses Kotlinmailer to send simple text emails. Now that you’ve covered the basics, you're ready to explore what else you can do with Kotlinmailer.

Last modified: 01 September 2024