scuba
scuba

Reputation: 183

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?

Upvotes: 18

Views: 7853

Answers (4)

CommonsWare
CommonsWare

Reputation: 1006859

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.

Upvotes: 29

Hamid Raza Goraya
Hamid Raza Goraya

Reputation: 159

For Kotlin use this extention

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

}

Upvotes: 1

Sam
Sam

Reputation: 42387

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

Upvotes: 1

jitter
jitter

Reputation: 54605

Did you try

Intent intent = new Intent(
    Intent.ACTION_SENDTO,
    Uri.parse("mailto:[email protected]")
);
startActivity(intent);

Upvotes: 6

Related Questions