Skip to main content

Kotlin Email Library

Official MailSlurp client for Kotlin. Create email accounts with real email addresses. Send and receive emails and attachments from Kotlin tests and code.

WARNING: Please use the Java version instead mailslurp-client-java as the Kotlin library cannot handle API methods that return generic lists at this stage.

Install

The MailSlurp Kotlin package is hosted on Maven Central.

Maven

<dependency>
<groupId>com.mailslurp</groupId>
<artifactId>mailslurp-client-kotlin</artifactId>
<version>${version}</version>
</dependency>

Gradle

implementation 'com.mailslurp:mailslurp-client-kotlin:version'

Configure

MailSlurp for Kotlin provides API controllers that match the REST API for MailSlurp. Each controller must be provided with an API key. Create a free MailSlurp account to obtain an API key.

val apiKey: String = System.getenv("API_KEY")
val inboxController = InboxControllerApi(apiKey)

Example

package com.mailslurp.examples

import com.mailslurp.apis.InboxControllerApi
import com.mailslurp.apis.WaitForControllerApi
import com.mailslurp.models.SendEmailOptions
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import kotlin.test.assertEquals
import kotlin.test.assertTrue

@RunWith(JUnit4::class)
class MailSlurpKotlinTest {

private val apiKey: String by lazy { System.getenv("API_KEY") }

@Test
fun `can create inboxes`() {
val inboxController = InboxControllerApi(apiKey)
val inbox = inboxController.createInbox(null, null, null, null, null, null, null, null, null)
assertTrue(inbox.emailAddress?.contains("@mailslurp") ?: false)
}

@Test
fun `can send and receive email`() {
// create inbox
val inboxController = InboxControllerApi(apiKey)
val waitForController = WaitForControllerApi(apiKey)
val inbox = inboxController.createInbox(null, null, null, null, null, null, null, null, null)

val testSubject = "test-subject"
val confirmation = inboxController.sendEmailAndConfirm(
inboxId = inbox.id!!,
sendEmailOptions = SendEmailOptions(
to = listOf(inbox.emailAddress!!),
subject = testSubject
)
)
assertEquals(confirmation.inboxId, inbox.id)

val email = waitForController.waitForLatestEmail(
inboxId = inbox.id!!,
timeout = 60_000,
unreadOnly = true
)
assertTrue(email.subject == "test-subject")
}
}