Intent.EXTRA_EMAIL not populating the To field

前端 未结 6 1750
有刺的猬
有刺的猬 2021-02-03 16:35

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.

6条回答
  •  半阙折子戏
    2021-02-03 17:07

    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.

提交回复
热议问题