0

I am having an issue with Email Intent in Android.

The recipients field is not populating properly.

My code is as such:

Extensions.kt

// Returns a Mail Intent
fun requireMailIntent(subject: String, body: String) = Intent(Intent.ACTION_SENDTO).apply {
    data = Uri.parse("mailto:")
    putExtra(Intent.EXTRA_EMAIL, arrayOf("email@gmail.com"))
    putExtra(Intent.EXTRA_SUBJECT, subject)
    putExtra(Intent.EXTRA_TEXT,body)
}



/**
 *  Checks whether the intent has an app that can
 *  handle it.
 *  Should be called before starting an intent
**/
fun Intent.hasSuccessor(context: Context) = resolveActivity(context.packageManager) != null

Fragment.kt

// Submit button
    binding.ButtonSubmit.setOnClickListener {
        val emailConstruct = constructEmail()
        val intent = requireMailIntent(emailConstruct.first, emailConstruct.second)
        if(intent.hasSuccessor(requireContext())){
            Log.v("INTENT_TEST", "Launching Intent")
            startActivity(intent)
        }else{
            Log.v("INTENT_TEST", "No app found")
        }
    }

private fun constructEmail(): Pair<String,String>{
    val subject = "MES :: Bug Report :: ${viewModel.bugIdentified}"
    val message = "Below are the steps \n ${viewModel.bugSteps}"

    return Pair(subject, message)
}

Manifest.xml

<!-- For basic package querying, ie browsers, email... -->
<uses-permission
    android:name="android.permission.QUERY_ALL_PACKAGES"
    tools:ignore="QueryAllPackagesPermission" />

Gradle

    // SDK Versions
    sdk_compiled_version = 30
    sdk_minimum_version = 24

    // Build tools
    build_tools = "29.0.3"

Upon clicking the button, Android shows me the app chooser, but when i click Gmail, everything populates except the recipients field.

Can someone please help ?

Mervin Hemaraju
  • 1,921
  • 2
  • 22
  • 71

1 Answers1

1

Try using URI

val uriText = "mailto:contact@example.com" +
            "?subject=" + "your subject here" +
            "&body=" + body
    val uri = Uri.parse(uriText)
    val sendIntent = Intent(Intent.ACTION_SENDTO)
    sendIntent.data = uri
    startActivity(Intent.createChooser(sendIntent, "Send Email").addFlags(FLAG_ACTIVITY_NEW_TASK))
Omar Qadomi
  • 43
  • 1
  • 7
  • Using it like this will complicate the recipients part as it is dynamic. Using an array will be simple to just push it in. Why is my code not working though? This is found in the android docs. – Mervin Hemaraju Mar 02 '21 at 18:14