2

I have been using below code to start an Intentin Android to send an email. Prior to Android Lollipop (API level 21) this worked fine. Unfortunately, in Android Lollipop, this throws an "Unsupported Action" error.

Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setType("message/rfc822");
intent.setData(Uri.parse("mailto:" + email));
startActivity(intent);

It's pretty basic, it simply passes the e-mailaddress and lets the user pick which application to use.

How should I adapt my code to make this work across api levels? My minimum API level is 16 (JellyBean).

Edit I've included the MIME-type, as per the comments and answers.

Benjamin
  • 1,983
  • 23
  • 33

4 Answers4

5

I've got it. This was caused by not having set up an emailaccount. After setting one up in at least one email app, it works.

It's not a problem with Lollipop.

Benjamin
  • 1,983
  • 23
  • 33
0

You have to add intent.setType("message/rfc822"); see this detailed answer: How can I send emails from my Android application?

Community
  • 1
  • 1
rickyalbert
  • 2,552
  • 4
  • 21
  • 31
  • Nope, still throws "Unsupported Action". – Benjamin Nov 03 '14 at 12:07
  • Did you copy the code in the answer? I see that you use ACTION_SENDTO while the link uses ACTION_SEND – rickyalbert Nov 03 '14 at 12:16
  • @rickalbert, I'll update my question to include the MIME type. I'm using ACTION_SENDTO because I'm not sending an attachment, as per the [documentation](https://developer.android.com/guide/components/intents-common.html#Email). – Benjamin Nov 03 '14 at 12:19
0

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

Sam
  • 40,644
  • 36
  • 176
  • 219
-1

By default this intent will be consumed by Android beam, I don't expect this behavior so I believe there must be something wrong in Lollipop.

ttanxu
  • 46
  • 4
  • Hi and welcome to Stackoverflow. Your question makes a serious claim and I think you should try to verify this with some resources and generally expand on your answer a bit. We are looking for professional references here. Thanks! – avalancha Nov 11 '14 at 18:19
  • As per my own answer: I had not set up any emailaccounts on the device. Hence, Android was unable to find any apps that could do something with my intent. That's what caused the behaviour. – Benjamin Nov 12 '14 at 10:53
  • @BenjaminTodts Android beam claims this intent can be handled by itself so even if you added an email account Android Beam is still a choice in the app chooser if you don't have a default app that'll consume this intent. – ttanxu Aug 09 '15 at 19:11
  • [intent filter](http://developer.android.com/guide/topics/manifest/intent-filter-element.html) is used in AndroidManifest by apps to tell os it can consume this intent properly. Android Beam must have been registered an intent filter that's too generic so that os thinks it can consume an email intent. – ttanxu Aug 09 '15 at 19:19