Android: Email Intent ACTION_SENDTO doesn't work unless you add an intent filter

Viewed 806

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?

1 Answers

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);
}
Related