One lifecycle handle
Each account is a Mailbox with a StateFlow of its state, generation-safe automatic reconnects and an idempotent close().
Mailbox lifecycle
Coroutine-first IMAP and SMTP with hosted OAuth2 for Gmail and Outlook. Read, watch and send mail from your backend with explicit lifecycles, typed failures and no hidden persistence.
gmail.open(MailAddress("alice@gmail.com")).use { mailbox ->
val inbox = checkNotNull(mailbox.folders.special(SpecialUse.INBOX)).path
mailbox.messages.watchEnvelopes(inbox, MessageQuery(seen = false)).collect { watched ->
val structure = mailbox.messages.structure(watched.envelope.location)
structure.attachments.filter { it.isPdf }.forEach { part ->
val pdf = mailbox.messages.download(part.ref, maxBytes = 10L * 1024 * 1024)
store(pdf.content.toByteArray())
}
saveCheckpoint(watched.next) // only after your processing is durable
}
}