jvmMain.com.bselzer.ktx.intent.email.Emailer.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of intent-jvm Show documentation
Show all versions of intent-jvm Show documentation
Based on Android intents.
The newest version!
package com.bselzer.ktx.intent.email
import com.bselzer.ktx.intent.JvmIntent
import com.bselzer.ktx.logging.Logger
import java.awt.Desktop
import java.net.URI
import java.net.URLEncoder
import java.nio.charset.StandardCharsets
internal actual class SystemEmailer actual constructor() : JvmIntent(), Emailer {
override fun send(email: Email): Boolean {
return try {
val desktop = desktop ?: return false
if (!desktop.isSupported(Desktop.Action.MAIL)) {
return false
}
// TODO is there a standard way to handle mailto creation to avoid doing all of this?
fun String.encoded() = URLEncoder.encode(this, StandardCharsets.UTF_8).replace("+", "%20")
val mailto = buildString {
val separator = ","
append("mailto:?to=")
append(email.to.joinToString(separator = separator))
append("&cc=")
append(email.cc.joinToString(separator = separator))
append("&bcc=")
append(email.bcc.joinToString(separator = separator))
append("&subject=")
append(email.subject?.encoded() ?: "")
append("&body=")
append(email.body?.encoded() ?: "")
}
val uri = URI.create(mailto)
desktop.mail(uri)
true
} catch (ex: Exception) {
Logger.e(ex) { "Failed to send $email" }
false
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy