I use intent.setData(Uri.parse("mailto:")); can't open gmail appp, also when i use intent.setType("text/plain"); I can't pre-fill the recipient's email
I tried to install another virtual device, another android abnr but it didn't work
CodePudding user response:
First check the virtual device that it has any mail App install in it. if not then try it with real mobile device or emulator which has an email app install in it.
Codes are given below.
Java version
Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
emailIntent.setData(Uri.parse("mailto:[email protected]"));
startActivity(Intent.createChooser(emailIntent, "Send feedback"));
or
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:")); // only email apps should handle this
intent.putExtra(Intent.EXTRA_EMAIL, address);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
Kotlin version
val emailIntent = Intent(Intent.ACTION_SENDTO).apply {
data = Uri.parse("mailto:[email protected]")}
startActivity(Intent.createChooser(emailIntent, "Send feedback"))
This was my output (only Gmail Inbox suggested):
I got this solution from the Android Developers site.
CodePudding user response:
you can use below snippet to send a Email from app - Kotlin
val intent = Intent(Intent.ACTION_SENDTO)
intent.data = Uri.parse("mailto:") // only email apps should handle this
intent.putExtra(
Intent.EXTRA_EMAIL,
arrayOf("[email protected]", "[email protected]")
)
intent.putExtra(Intent.EXTRA_SUBJECT, "Stackoverflow Issue")
intent.putExtra(Intent.EXTRA_TEXT, "E-mail body");
startActivity(Intent.createChooser(intent, "Send Email Using..."));
If more then One app there to handle SENDTO intent, it will show options to select app. otherwise, it will open the available mail app directly.