Intent URI to launch Gmail App

Viewed 84424

Is there any URI which can point to the GMAIL App in android and help me launch it?

19 Answers

The best way to do it, is by using the generic way/method:

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("plain/text");
intent.putExtra(Intent.EXTRA_SUBJECT, "Email Subject goes here");
intent.putExtra(Intent.EXTRA_TEXT, "Your Message goes here");
startActivity(Intent.createChooser(intent, ""));

This will give the users a choice where they can pick GMail (if installed) or any other email supporting app they have.

startActivity(new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:"+mailId)));

Working as for me is just simple like this:

 Intent(Intent.ACTION_SEND).apply{
            setPackage("com.google.android.gm")
            type = "text/plain"
            putExtra(Intent.EXTRA_TEXT, "Go, go share text!")
        }.also{readyIntent->
            startActivity(readyIntent)
        }

This snippet will open a chooser which is supposed to point to Gmail inbox.

val intent = Intent(Intent.ACTION_MAIN)
intent.addCategory(Intent.CATEGORY_APP_EMAIL)
try {
    startActivity(Intent.createChooser(intent, getString(R.string.open_email_app)))
            } catch (e: ActivityNotFoundException) {
    showErrorDialog(R.string.error_activity_is_not_found)
            }

Neither was working for me, as the subject and message were always empty, until I found the official solution here.

fun composeEmail(addresses: Array<String>, subject: String, attachment: Uri) {
    val intent = Intent(Intent.ACTION_SEND).apply {
        type = "*/*"
        putExtra(Intent.EXTRA_EMAIL, addresses)
        putExtra(Intent.EXTRA_SUBJECT, subject)
        putExtra(Intent.EXTRA_STREAM, attachment)
    }
    if (intent.resolveActivity(packageManager) != null) {
        startActivity(intent)
    }
}

This answer is old,but still appears in the Google Search at first position.

Hence,from the android documentation,the better way to do this now is :

public void composeEmail(String[] addresses, String subject, Uri attachment) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_EMAIL, addresses);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_STREAM, attachment);
if (intent.resolveActivity(getPackageManager()) != null) {
    startActivity(intent);
}

}

More information can be found out at here

final String package = "com.google.android.gm";
// return true if gmail is installed
boolean isGmailInstalled = isAppInstalled(MainActivity.this, package);

Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_EMAIL, new String[] {"mail@gamil.com"});
if (isGmailInstalled) {
    intent.setType("text/html");
    intent.setPackage(package);
    startActivity(intent);
} else {  // allow user to choose a different app to send email with
    intent.setType("message/rfc822");
    startActivity(Intent.createChooser(intent, "choose an email client"));
}

// Method to check if app is installed
private boolean isAppInstalled(Context context, String packageName) {
    try {
        context.getPackageManager().getApplicationInfo(packageName, 0);
        return true;
    } catch (PackageManager.NameNotFoundException e) {
        return false;
    }
}

Intent URI to launch Gmail App

Intent mailClient = new Intent(Intent.ACTION_VIEW);
mailClient.setClassName("com.google.android.gm", "com.google.android.gm.ConversationListActivityGmail");
startActivity(mailClient);
Related