Skip to content Skip to sidebar Skip to footer

Opening Email Client Via Intent (but Not To Send A Message)

Is there a way to programically open email client, without a need to forcing message send? I just want the app to let user open his email client for email checking purposes :)

Solution 1:

    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

Solution 2:

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

try {
    List<String> emailClientNames = newArrayList<String>();
    final List<String> emailClientPackageNames = newArrayList<String>();
    // finding list of email clients that support send emailIntentintent=newIntent(Intent.ACTION_SENDTO, Uri.fromParts(
        "mailto", "abc@gmail.com", null));
    PackageManagerpkgManager= AppController.getContext().getPackageManager();
    List<ResolveInfo> packages = pkgManager.queryIntentActivities(intent, 0);
    if (!packages.isEmpty()) {
        for (ResolveInfo resolveInfo : packages) {
            // finding the package nameStringpackageName= resolveInfo.activityInfo.packageName;
            emailClientNames.add(resolveInfo.loadLabel(getPackageManager()).toString());
            emailClientPackageNames.add(packageName);
        }
        // a selection dialog  for the email clients
        AlertDialog.Builderbuilder=newAlertDialog.Builder(MyActivity.this);
        builder.setTitle("Select email client");
        builder.setItems(emailClientNames.toArray(newString[]{}), newDialogInterface.OnClickListener() {
        publicvoidonClick(DialogInterface dialog, int which) {
            // on click we launch the right packageIntentintent= getPackageManager().getLaunchIntentForPackage(emailClientPackageNames.get(which));
                startActivity(intent);
            }
        });
        AlertDialogdialog= builder.create();
        dialog.show();
    }
} catch (ActivityNotFoundException e) {
    // Show error message
}

Solution 3:

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.

funemailAppIntent(): 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 chooserIntentval 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 chooserval emailInboxIntents = mutableListOf<LabeledIntent>()
        for (i in1 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 {
        returnnull
    }
}

.. then later call it with

valemailChooserIntent= emailAppIntent()

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

or handle null however you want.

Solution 4:

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:

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

}

Solution 5:

A bit more modern solution in Kotlin

funtryVerifyMail() {
        try {
            val intents: List<Intent> = (packageManager.queryIntentActivities(Intent(
                Intent.ACTION_SENDTO, Uri.fromParts(
                    "mailto", "lowhillgamesoy@gmail.com", null
                )
            ), 0) + packageManager.queryIntentActivities(Intent(Intent.ACTION_VIEW).also {
                it.type = "message/rfc822"
            }, 0)).mapNotNull {
                it.activityInfo.packageName
            }.toSet().mapNotNull {
                packageManager.getLaunchIntentForPackage(it)
            }

            if(intents.size > 0) {
                startActivityForResult(Intent.createChooser(intents.first(), getString(R.string.verify_mail)).also {
                    if(intents.size > 1) {
                        it.putExtra(Intent.EXTRA_INITIAL_INTENTS, intents.subList(1, intents.size - 1).toTypedArray())
                    }
                }, OPEN_MAIL_REQUEST_CODE)
            } else {
                Toast.makeText(this, "Verify your e-mail by clicking the link in your e-mail inbox.", Toast.LENGTH_LONG).show()
            }


        } catch (e: ActivityNotFoundException) {
            // Show error message
            e.printStackTrace()
        }
    }

This also picks both mail clients only supporting ACTION_SEND and ACTION_VIEW and removes duplicates.

Post a Comment for "Opening Email Client Via Intent (but Not To Send A Message)"