Android Studio mailto Intent doesn't show subject and mail body

前端 未结 5 1746
無奈伤痛
無奈伤痛 2020-12-11 02:45

I\'m trying to send an e-mail from my Android App. With the click on a button, gmail should open and show a new email with my previously defined recipient, subject and email

5条回答
  •  鱼传尺愫
    2020-12-11 03:20

    In order to get it to work on Samsung and Pixel devices, we had to add the parameters on both the url and the extras

    val email = "xxxx@xxxx.com"
    val subject = "xxxx"
    val body = "xxxx"
    
    val selectorIntent = Intent(Intent.ACTION_SENDTO)
    val urlString = "mailto:" + Uri.encode(email) + "?subject=" + Uri.encode(subject) + "&body=" + Uri.encode(body)
    selectorIntent.data = Uri.parse(urlString)
    
    val emailIntent = Intent(Intent.ACTION_SEND)
    emailIntent.putExtra(Intent.EXTRA_EMAIL, arrayOf(email))
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject)
    emailIntent.putExtra(Intent.EXTRA_TEXT, body)
    emailIntent.selector = selectorIntent
    
    startActivity(Intent.createChooser(emailIntent, "Send email"))
    

提交回复
热议问题