Skip to content

Introduction ​

MailKT is a coroutine-first Kotlin/JVM library for reading, watching and sending email through a managed Mailbox, with hosted OAuth2 for Gmail and Outlook (Microsoft 365).

It runs entirely inside your backend process. MailKT never opens a browser, never starts an HTTP listener and never persists anything on its own. Everything that has to outlive a process (tokens, pending authorizations, checkpoints) crosses small interfaces or comes back as plain values you store.

Why MailKT ​

Talking to IMAP directly is full of sharp edges: connections drop silently, UIDs become meaningless when UIDVALIDITY changes, attachments are downloaded whole just to read a file name, and a lost connection during SMTP DATA leaves you guessing whether the mail went out. MailKT turns each of these into an explicit, typed part of the API.

ConcernWhat MailKT does
LifecycleOne Mailbox per account with a StateFlow<MailboxState>, automatic generation-safe reconnects and idempotent close().
ReadingEnvelope, then structure, then selected parts. Filtering happens before any content is fetched.
IntegrityMessage and part references carry UIDVALIDITY. A changed folder yields a typed failure, never a different message.
WatchingIMAP IDLE with catch-up from a checkpoint, at-least-once delivery and in-session duplicate suppression.
SendingOutcomes are ACCEPTED, FAILED or UNKNOWN and are never retried automatically.
ModelsImmutable MailKT types only. No Jakarta Mail or Angus type appears in the public API.
PrivacyAddresses, subjects, bodies and tokens are redacted from toString(), exception messages and logs.

Modules ​

ModulePurpose
coreMailbox, models, capabilities, lifecycle, IMAP/SMTP transport, MIME.
gmailHosted Google authorization-code flow and imap.gmail.com / smtp.gmail.com.
outlookHosted Microsoft (MSAL4J) flow and outlook.office365.com IMAP/SMTP.
examplesCompile-tested sources of every snippet in this documentation.

Snippets are compiled

The Kotlin examples in the guides are imported directly from the examples module of the repository, which is compiled and tested on every build, so they cannot drift from the API.

How the pieces fit ​

How the provider client, Mailbox, your stores and the mail servers relateYOUR BACKENDMAILKTPROVIDERTokenStoreopaque token bytesAuthorizationSessionStorepending authorizationsYour codeprocessing, checkpointsGmail / Outlookauthorize, refresh tokensMailboxfoldersmessagesconversationsoutboxOAuth serverGoogle · Microsoft EntraMail serversIMAP · SMTPOAuth2open(email)XOAUTH2Flow
Lifecycle handleMailKTYou implementExternal

A provider client (Gmail or Outlook) handles authorization and token refresh. open turns stored tokens into a Mailbox, which owns the connection, its recovery loop and four capabilities:

Next steps ​

Released under the MIT License.