I tried this code which I found here:
Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto", "[email protected]", null)); startActivity(intent);
But I get a message on the screen which reads "Unsupported Action". Any ideas of how to get this working?
Try this snippet by dylan:
/* Create the Intent */
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
/* Fill it with Data */
emailIntent.setType("plain/text");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"[email protected]"});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Text");
/* Send it off to the Activity-Chooser */
context.startActivity(Intent.createChooser(emailIntent, "Send mail..."));
Key pieces: using EXTRA_EMAIL
for your addresses and using createChooser()
in case the user has more than one email client configured.
Did you try
Intent intent = new Intent(
Intent.ACTION_SENDTO,
Uri.parse("mailto:[email protected]")
);
startActivity(intent);
I think the real problems here are that you're running on the official emulator and your intent isn't matching anything.
From my testing, this is a problem that happens when the intent's URI (from setData()
) doesn't match anything and you're running on one of the official Android emulators. This doesn't seem to happen on real devices, so it shouldn't be a real-world problem.
You can use this code to detect when this is going to happen before you launch the intent:
ComponentName emailApp = intent.resolveActivity(getPackageManager());
ComponentName unsupportedAction = ComponentName.unflattenFromString("com.android.fallback/.Fallback");
boolean hasEmailApp = emailApp != null && !emailApp.equals(unsupportedAction);
(The name of the activity that shows the "Unsupported action" action method is com.android.fallback.FallbackActivity
.)
For Kotlin use this extention
fun Context.sendEmailTo(email:String){
Intent(Intent.ACTION_SENDTO).apply {
data = Uri.parse("mailto:$email")
startActivity(this)
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With