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: Library Overview and Practical Examples



Kotlin is a modern programming language actively used for creating server applications, backend, and microservices. The ability to programmatically send emails is one of the key functions for many projects: notifications, registration confirmations, newsletters. In this article, we will cover how to organize email sending in Kotlin, which libraries to use, and how to configure an SMTP connection.



Main Ways to Send Email in Kotlin



Since Kotlin runs on the JVM, you can use any Java libraries. The most popular and proven method is JavaMail (Jakarta Mail). An alternative option is Ktor Client (part of the Ktor framework), which can work with SMTP directly. Let's look at both approaches.



1. Sending Email with Jakarta Mail (JavaMail)



Jakarta Mail (formerly JavaMail) is the standard library for working with email in the Java ecosystem. It is perfectly suited for Kotlin as it is fully compatible with the JVM. You will need to add the dependency in build.gradle.kts:



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


After adding the library, you can write a simple sending function:



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

fun sendEmail( host: String, port: Int, username: String, password: String, from: String, to: String, subject: String, body: String) { val props = Properties().apply { put("mail.smtp.host", host) put("mail.smtp.port", port.toString()) put("mail.smtp.auth", "true") put("mail.smtp.starttls.enable", "true") }

val session = Session.getInstance(props, object : Authenticator() { override fun getPasswordAuthentication(): PasswordAuthentication { return PasswordAuthentication(username, password) } })

try { val message = MimeMessage(session).apply { setFrom(InternetAddress(from)) setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)) setSubject(subject) setText(body) } Transport.send(message) println("Email successfully sent to $to") } catch (e: MessagingException) { println("Sending error: ${e.message}") }}


Function call:



fun main() {    sendEmail(        host = "smtp.gmail.com",        port = 587,        username = "youremail@gmail.com",        password = "your-app-password",        from = "youremail@gmail.com",        to = "recipient@example.com",        subject = "Hello from Kotlin!",        body = "This is a test email sent via Kotlin and Jakarta Mail."    )}


2. Sending Email with Attachments (Jakarta Mail)



To send an email with a file, you need to use MimeBodyPart and MimeMultipart:



import jakarta.mail.internet.MimeBodyPartimport jakarta.mail.internet.MimeMultipartimport java.io.File

fun sendEmailWithAttachment( session: Session, from: String, to: String, subject: String, body: String, attachmentPath: String) { val message = MimeMessage(session).apply { setFrom(InternetAddress(from)) setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)) setSubject(subject) }

// Text part val textPart = MimeBodyPart().apply { setText(body) }

// Attachment val attachmentPart = MimeBodyPart().apply { attachFile(File(attachmentPath)) }

// Assemble multipart val multipart = MimeMultipart().apply { addBodyPart(textPart)

Recommendations