I am trying to use an intent to send an email from my application but the To field of the email will not populate. If I add code to fill in the subject or text, they work fine.
In Kotlin - Android
fun sendMail(
activity: Activity,
emailIds: Array,
subject: String,
textMessage: String
) {
val emailIntent = Intent(Intent.ACTION_SEND)
emailIntent.type = "text/plain"
emailIntent.putExtra(Intent.EXTRA_EMAIL, emailIds)
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject)
emailIntent.putExtra(Intent.EXTRA_TEXT, textMessage)
emailIntent.setType("message/rfc822")
try {
activity.startActivity(
Intent.createChooser(
emailIntent,
"Send email using..."
)
)
} catch (ex: ActivityNotFoundException) {
Toast.makeText(
activity,
"No email clients installed.",
Toast.LENGTH_SHORT
).show()
}
}
Also you can use [
val emailIntent = Intent(Intent.ACTION_SENDTO)
] to invoke direct email client
//argument of function
val subject = "subject of you email"
val eMailMessageTxt = "Add Message here"
val eMailId1 = "emailId1@gmail.com"
val eMailId2 = "emailId2@gmail.com"
val eMailIds: Array = arrayOf(eMailId1,eMailId2)
//Calling function
sendMail(this, eMailIds, subject, eMailMessageTxt)
I hope this code snippet will help to kotlin developers.