2

I want to open Gmail with a preformatted email. I am using this code:

public static void sendEmail(Context context, String receiverAddress, String title, String body) {
    Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
    emailIntent.setData(Uri.parse("mailto:"));
    emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { receiverAddress });
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, title);
    if (body != null) {
        emailIntent.putExtra(Intent.EXTRA_TEXT, body);
    }
    if (emailIntent.resolveActivity(context.getPackageManager()) != null) {
        context.startActivity(emailIntent);
    }
}

However it works only if I add this intent-filter to the manifest file of my app:

<intent-filter>
    <action android:name="android.intent.action.SENDTO" />
    <data android:scheme="mailto" />
    <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

By doing this, it shows me an app selector with two apps: my app and Gmail.

However I don't want my app to be the receiver of this intent. I just want this intent received by Gmail (and other email clients). But if I don't add that intent-filter, nothing happens.

What am I doing wrong?

Daniele B
  • 19,801
  • 29
  • 115
  • 173
  • 2
    https://stackoverflow.com/q/62535856 – Mike M. Jun 24 '21 at 17:31
  • 2
    I recommend removing the `resolveActivity()` call and replacing it with a `try`/`catch` around the `startActivity()` call. You need that `try`/`catch` anyway, as there may be other problems when starting an activity from another app. Also note that none of those extras are documented for `ACTION_SENDTO`, so not every app will honor them. – CommonsWare Jun 24 '21 at 17:36
  • @CommonsWare you were right, by removing `resolveActivity()` the issue is gone. However I wonder why Android official documention says to use it: https://developer.android.com/guide/components/intents-common#Email – Daniele B Jun 24 '21 at 17:51
  • 2
    Prior to Android 11, `resolveActivity()` wasn't a problem. That particular documentation page was not updated when Android 11 shipped. – CommonsWare Jun 24 '21 at 17:54

2 Answers2

0

Can you try the following? This is what I use. As far as I can see your code is fine, and the chooser thing should not impact anything as such I feel, but still I would recommend to try the following once. It could be the chooser that is causing issue I feel.

public void composeEmail(String[] addresses, String subject) {
    Intent sendIntent = new Intent(Intent.ACTION_SENDTO);
    sendIntent.setData(Uri.parse("mailto:")); // only email apps should handle this
    sendIntent.putExtra(Intent.EXTRA_EMAIL, addresses);
    sendIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
    Intent shareIntent = Intent.createChooser(sendIntent, null);
    startActivity(shareIntent);
}
che10
  • 2,176
  • 2
  • 4
  • 11
  • 1
    it seems I don't even need the `Intent.createChooser` line. I can just put at `try`/`catch` around the `startActivity`, as @CommonsWare suggested in his comment – Daniele B Jun 24 '21 at 17:54
  • 1
    Yes, that is one way too. Although I would suggest you use a chooser, you can supply it a message too if no app is matching and the common share sheet will handle it. @DanieleB – che10 Jun 24 '21 at 18:01
0

I had the SENDTO intent-filter as this and my app was appearing in the list of email clients when startActivity called.

 <intent-filter>
      <action android:name="android.intent.action.SENDTO" />
      <data android:scheme="mailto" />
      </intent-filter>

I removed the mailto

 <intent-filter>
      <action android:name="android.intent.action.SENDTO" />
 </intent-filter>

and my app stopped appearing in the list of mail clients.

I didn't change anything with resolveActivity or startActivity.

brian.clear
  • 5,277
  • 2
  • 41
  • 62