Search code examples
androidandroid-intentandroid-12

How to include email recipient information in intents on Android 12?


Ive been using the recommended way to populate an email intent since the early days of Android. This includes recipient, subject, and body text.

On Android 12 however - the recipient field is always left out when doing this, everything else works exactly the same.

Whats is the issue here? Is this a bug in the OS? Im testing with gmail by default, but the same thing applies to other email clients, still only on Android 12.

private fun createIntent(
    metadata: String
): Intent {
    val uri = Uri.parse("mailto:")

    return Intent(ACTION_SENDTO)
        .setData(uri)
        .putExtra(
            EXTRA_EMAIL,
            arrayOf("[email protected]") //Ive also tried without arrayOf, no difference.
        )
        .putExtra(
            EXTRA_SUBJECT,
            "Feedback"
        )
        .putExtra(
            EXTRA_TEXT,
            metadata
        )
}

Solution

  • It seems that using ACTION_SEND in combination with the selector block does the trick. This works across all API levels down to API 21. Ill let someone else explain why that is, but the important thing is that it works.

    private fun createIntent(
        metadata: String
    ): Intent {
        return Intent(ACTION_SEND)
            .putExtra(
                EXTRA_EMAIL,
                arrayOf(EMAIL)
            )
            .putExtra(
                EXTRA_SUBJECT,
                TITLE
            )
            .putExtra(
                EXTRA_TEXT,
                metadata
            )
            .apply {
                selector = Intent(ACTION_SENDTO).setData(Uri.parse("mailto:"))
            }
    }