Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I launch the email app with the "to" field pre-filled?

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?

like image 226
scuba Avatar asked Nov 20 '09 19:11

scuba


4 Answers

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.

like image 58
CommonsWare Avatar answered Nov 20 '22 22:11

CommonsWare


Did you try

Intent intent = new Intent(
    Intent.ACTION_SENDTO,
    Uri.parse("mailto:[email protected]")
);
startActivity(intent);
like image 20
jitter Avatar answered Nov 20 '22 21:11

jitter


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.)

like image 1
Sam Avatar answered Nov 20 '22 20:11

Sam


For Kotlin use this extention

fun Context.sendEmailTo(email:String){
Intent(Intent.ACTION_SENDTO).apply {
    data = Uri.parse("mailto:$email")
    startActivity(this)
}

}

like image 1
Hamid Raza Goraya Avatar answered Nov 20 '22 20:11

Hamid Raza Goraya