Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Intent.ACTION_SENDTO produces error 'No apps can perform this action" even though there are two email clients installed

Tags:

android

I am attempting to use the email intent in Android Studio 3.01. If I use ACTION_SENDTO, I get an error No apps can perform this action even though the stock Android email client and the Gmail email app are both installed. If I use ACTION_SEND in place of ACTION_SENDTO, a screen is shown displaying every app on the device.

My goal is to invoke the default email client directly, without going through an intervening screen. What am I doing wrong?

The code I'm using is:

public void sendEmail(View view) {

    Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
    emailIntent.setData(Uri.parse("mailto:"));
    emailIntent.setType("text/plain");

    try {
        startActivity(Intent.createChooser(emailIntent, "Send mail..."));
        finish();
        Log.i("Email sent!", "");
    } catch (android.content.ActivityNotFoundException ex) {
        Toast.makeText(MapsActivityCurrentPlace.this,
                "Email not installed.", Toast.LENGTH_SHORT).show();
    }
}

EDITED

Thanks to the answer, final working code looks like:

public void sendEmail(View view) {

    Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
    emailIntent.setData(Uri.parse("mailto:"));
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
    emailIntent.putExtra(Intent.EXTRA_TEXT, "Message...");

    try {
        startActivity(emailIntent);
        finish();
        Log.i("Email sent!", "");
    } catch (android.content.ActivityNotFoundException ex) {
        Toast.makeText(MapsActivityCurrentPlace.this,
                "Email not installed.", Toast.LENGTH_SHORT).show();
    }
}

}

like image 990
hermitjimx Avatar asked Sep 12 '25 12:09

hermitjimx


1 Answers

First, ACTION_SENDTO does not take a MIME type. So, get rid of setType(). That solves two problems:

  1. You are artificially restricting to apps that would claim to support that MIME type

  2. setType() wipes out your setData() call (setType(type) is the same as setDataAndType(null, type))

Second, if your goal is to launch an email client directly, get rid of createChooser().

like image 63
CommonsWare Avatar answered Sep 15 '25 04:09

CommonsWare