1

From my app, I need to invoke an email client to send an email with attachment. I do not have the destination email address - that's something the user would be entering themselves. I used to have this code:

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setDataAndType(Uri.parse("mailto:"), "text/plain");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.putExtra(Intent.EXTRA_EMAIL, "");
intent.putExtra(Intent.EXTRA_SUBJECT, emailSubject);
intent.putExtra(Intent.EXTRA_TEXT, emailBody);
intent.putExtra(Intent.EXTRA_STREAM, attachmentUri);

try {
    context.get().startActivity(intent);
} catch (ActivityNotFoundException ex) {
    ...
}

This worked perfectly fine up until Android 11. With Android 11, I get ActivityNotFoundException. Now, if I remove the "data" element and replace call to setDataAndType to simply setType("text/plain"), then I get the activity selection popup to choose from, however it contains all activities and applications that support SEND action. How do I get to filter this list to email clients only?

Aleks G
  • 56,435
  • 29
  • 168
  • 265
  • 1
    "This worked perfectly fine up until Android 11" -- not reliably, as `ACTION_SEND` does not take a `Uri`. Your code is relying on bugs in email apps, where their `` happens to support a `Uri` scheme for `ACTION_SEND`. [This specific answer](https://stackoverflow.com/a/59365539/115145) on the duplicate question works. – CommonsWare Apr 21 '21 at 10:41
  • 1
    According to the docs (https://developer.android.com/guide/components/intents-common#Email) sending email does support URI I've just run into this issue (I'm not adding Uri, I'm only trying to sent text as email), I've tried `Intent.ACTION_SEND` and `Intent.ACTION_SENDTO` and it keeps throwing `ActivityNotFoundException` when I set the data `mailTo:`... – ProjectDelta Nov 18 '21 at 10:58

0 Answers0