Opening email client via Intent (but not to send a message)

In Kotlin, but this version creates a chooser for user to pick which email app to use. You basically do what Oved does in his answer, except create an actual chooser using LabeledIntent.

fun emailAppIntent(): Intent? {
    val emailIntent = Intent(Intent.ACTION_VIEW, Uri.parse("mailto:"))
    val packageManager = appLaunchContext.packageManager

    val activitiesHandlingEmails = packageManager.queryIntentActivities(emailIntent, 0)
    if (activitiesHandlingEmails.isNotEmpty()) {
        // use the first email package to create the chooserIntent
        val firstEmailPackageName = activitiesHandlingEmails.first().activityInfo.packageName
        val firstEmailInboxIntent = packageManager.getLaunchIntentForPackage(firstEmailPackageName)
        val emailAppChooserIntent = Intent.createChooser(firstEmailInboxIntent, "")

        // created UI for other email packages and add them to the chooser
        val emailInboxIntents = mutableListOf<LabeledIntent>()
        for (i in 1 until activitiesHandlingEmails.size) {
            val activityHandlingEmail = activitiesHandlingEmails[i]
            val packageName = activityHandlingEmail.activityInfo.packageName
            val intent = packageManager.getLaunchIntentForPackage(packageName)
            emailInboxIntents.add(
                LabeledIntent(
                    intent,
                    packageName,
                    activityHandlingEmail.loadLabel(packageManager),
                    activityHandlingEmail.icon
                )
            )
        }
        val extraEmailInboxIntents = emailInboxIntents.toTypedArray()
        return emailAppChooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraEmailInboxIntents)
    } else {
        return null
    }
}

.. then later call it with

val emailChooserIntent = emailAppIntent()

if (emailChooserIntent != null) {
    startActivity(emailAppIntent())
}

or handle null however you want.


This code will show a dialog with a list of email clients. Clicking one will launch the application:

try {
    List<String> emailClientNames = new ArrayList<String>();
    final List<String> emailClientPackageNames = new ArrayList<String>();
    // finding list of email clients that support send email
    Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
        "mailto", "[email protected]", null));
    PackageManager pkgManager = AppController.getContext().getPackageManager();
    List<ResolveInfo> packages = pkgManager.queryIntentActivities(intent, 0);
    if (!packages.isEmpty()) {
        for (ResolveInfo resolveInfo : packages) {
            // finding the package name
            String packageName = resolveInfo.activityInfo.packageName;
            emailClientNames.add(resolveInfo.loadLabel(getPackageManager()).toString());
            emailClientPackageNames.add(packageName);
        }
        // a selection dialog  for the email clients
        AlertDialog.Builder builder = new AlertDialog.Builder(MyActivity.this);
        builder.setTitle("Select email client");
        builder.setItems(emailClientNames.toArray(new String[]{}), new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            // on click we launch the right package
            Intent intent = getPackageManager().getLaunchIntentForPackage(emailClientPackageNames.get(which));
                startActivity(intent);
            }
        });
        AlertDialog dialog = builder.create();
        dialog.show();
    }
} catch (ActivityNotFoundException e) {
    // Show error message
}

I think you should replace Intent.ACTION_SEND to Intent.ACTION_VIEW,
i am sure this will work as this will prompt with list of application which support MIME type "message/rfc822" so it will include your default email client in your device other than gmail app.

How about this code:

final Intent emailLauncher = new Intent(Intent.ACTION_VIEW);
emailLauncher.setType("message/rfc822");
try{
       startActivity(emailLauncher);
}catch(ActivityNotFoundException e){

}

    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_APP_EMAIL);
    startActivity(intent);  
    startActivity(Intent.createChooser(intent, getString(R.string.ChoseEmailClient)));

That kinda worked. But it opend Gmail for me, even since I have other email clients