Sending Email in Kotlin: Libraries and Code Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Sending Email in Kotlin: A Complete Guide to Libraries and Tools



Sending email is one of the key tasks in modern web applications: from registration confirmation to notifications and marketing mailings. Kotlin, as a modern language for the JVM, provides several approaches for working with mail. In this article, we will cover three main options: the classic JavaMail, the asynchronous Ktor Client, and the cloud service SendGrid. You will learn how to send simple text and HTML emails, attach files, and handle errors.



1. JavaMail — A Proven Standard



JavaMail is a standard library for working with email in the Java/Kotlin ecosystem. It supports SMTP, POP3, and IMAP protocols, as well as SSL/TLS encryption. For a Kotlin project, simply add the dependency in build.gradle.kts:



dependencies {    implementation("com.sun.mail:jakarta.mail:2.0.1")}


Example of sending a simple text email via SMTP with authentication:



import jakarta.mail.*import jakarta.mail.internet.InternetAddressimport jakarta.mail.internet.MimeMessageimport java.util.Properties

fun sendEmailViaJavaMail() { val props = Properties().apply { put("mail.smtp.host", "smtp.gmail.com") put("mail.smtp.port", "587") put("mail.smtp.auth", "true") put("mail.smtp.starttls.enable", "true") }

val session = Session.getInstance(props, object : Authenticator() { override fun getPasswordAuthentication(): PasswordAuthentication { return PasswordAuthentication("your-email@gmail.com", "your-app-password") } })

try { val message = MimeMessage(session).apply { setFrom(InternetAddress("your-email@gmail.com")) setRecipient(Message.RecipientType.TO, InternetAddress("recipient@example.com")) subject = "Hello from Kotlin!" setText("This is a test email sent with JavaMail.") } Transport.send(message) println("Email sent successfully!") } catch (e: MessagingException) { println("Sending error: ${e.message}") }}


Important: For Gmail, you need to use an App Password, not your main password. You can also configure SMTP for Yandex, Mail.ru, or your own server.



2. Ktor Client — An Asynchronous Approach



If you are using the Ktor framework to build web applications, you can send email via HTTP requests to mail APIs (e.g., Mailgun, SendGrid, Postmark). This is convenient for asynchronous services and microservice architecture.



Add the dependency:



dependencies {    implementation("io.ktor:ktor-client-core:2.3.7")    implementation("io.ktor:ktor-client-cio:2.3.7")    implementation("io.ktor:ktor-client-content-negotiation:2.3.7")    implementation("io.ktor:ktor-serialization-kotlinx-json:2.3.7")}


Example of sending an email via the SendGrid API:



import io.ktor.client.*import io.ktor.client.call.*import io.ktor.client.plugins.contentnegotiation.*import io.ktor.client.request.*import io.ktor.http.*import io.ktor.serialization.kotlinx.json.*import kotlinx.serialization.Serializableimport kotlinx.serialization.json.Json

@Serializabledata class EmailRequest( val personalizations: List<Personalization>, val from: EmailAddress, val subject: String, val content: List<Content>)

@Serializabledata class Personalization(val to: List<EmailAddress>)

@Serializabledata class EmailAddress(val email: String)

@Serializabledata class Content(val type: String, val value: String)

suspend fun sendEmailViaSendGrid() { val client = HttpClient(CIO) { install(ContentNegotiation) {

Recommendations