41

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

Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
Sana
  • 9,895
  • 15
  • 59
  • 87

19 Answers19

74

This works to intent just the gmail app.

final Intent intent = new Intent(Intent.ACTION_VIEW)
    .setType("plain/text")
    .setData(Uri.parse("[email protected]"))
    .setClassName("com.google.android.gm", "com.google.android.gm.ComposeActivityGmail")
    .putExtra(Intent.EXTRA_EMAIL, new String[]{"[email protected]"})
    .putExtra(Intent.EXTRA_SUBJECT, "test")
    .putExtra(Intent.EXTRA_TEXT, "hello. this is a message sent from my demo app :-)");
startActivity(intent);

use for plenty of emails:

intent.putExtra(Intent.EXTRA_EMAIL, new String[] { "[email protected]" });

for single emails:

intent.setData(Uri.parse("[email protected]"));

You may add extra putExtra(Intent.EXTRA..) and change the setType for your purpose. :P

Update (1/22/14): It is important to note that if you are going to use this code, you check to make sure that the user has the package "com.google.android.gm" installed on their device. In any language, make sure to check for null on specific Strings and initializations.

Please see Launch an application from another application on Android

enter image description here

Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
42

I'm using this in my apps:

Intent mailClient = new Intent(Intent.ACTION_VIEW);
mailClient.setClassName("com.google.android.gm", "com.google.android.gm.ConversationListActivity");
startActivity(mailClient);
katzoft
  • 858
  • 8
  • 10
  • 1
    Excellent answer! I was looking for it since a long time. – Sana Aug 29 '10 at 17:47
  • 36
    That is undocumented, unsupported, and may well break in the future. – CommonsWare Sep 18 '11 at 12:28
  • 5
    Targeting API 17 I'm getting a `android.content.ActivityNotFoundException: Unable to find explicit activity class {com.google.android.gm/com.google.android.gm.ConversationListActivity}; have you declared this activity in your AndroidManifest.xml?` Any ideas? – Garret Wilson Nov 16 '12 at 21:59
  • Getting the launch intent for the package, as indicated [below](http://stackoverflow.com/a/7249579/421049), works for me. – Garret Wilson Nov 16 '12 at 22:13
  • 1
    Calling `setPackage("com.google.android.gm")`, which is available since ICS, is undocumented as well, but is very unlikely to break as the package name will probably not change. – caw May 30 '13 at 00:41
  • Undocummented way. You should not use this – Knuckles the Echidna Nov 11 '13 at 08:00
  • @CommonsWare Currently my implementation is showing the Google drive also. Is there any updated methods by which i can launch just the gmail app – Ajith M A Feb 11 '14 at 09:45
  • @garret use mailClient.setClassName("com.google.android.gm", "com.google.android.gm.ComposeActivityGmail"); – Ajay Pandya Mar 27 '15 at 12:48
  • 3
    Now its not working....can use: Intent intent = getPackageManager().getLaunchIntentForPackage("com.google.android.gm"); startActivity(intent); – Shivang Feb 18 '16 at 08:57
  • 7
    The 2nd parameter (`className`) is `"com.google.android.gm.ConversationListActivityGmail"` for the time of writing (Gmail v6.7, Marshmallow). – Quoting Eddie Aug 21 '16 at 14:44
23

Using package name is not recommended as its an undocumented method. In case if the package name changes some day the code will fail.

Try this code instead

 Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
 "mailto", "[email protected]", null));
 emailIntent.putExtra(Intent.EXTRA_SUBJECT, "This is my subject text");
 context.startActivity(Intent.createChooser(emailIntent, null));

Ref: http://developer.android.com/reference/android/content/Intent.html#ACTION_SENDTO\

Ajith M A
  • 3,838
  • 3
  • 32
  • 55
  • 1
    Use try catch block to prevent crash if package name changes in future. But setting package name gives better UX, so for gmail we should go with package – CopsOnRoad Feb 10 '18 at 07:07
21

Use this:

Intent intent = getPackageManager().getLaunchIntentForPackage("com.google.android.gm");
startActivity(intent);

This could be device and API level dependent. Use with care.

CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
Richard Lalancette
  • 2,401
  • 24
  • 29
17

Simple and 100 % working

Intent intent = new Intent (Intent.ACTION_SEND);
intent.setType("message/rfc822");
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"[email protected]"});
intent.putExtra(Intent.EXTRA_SUBJECT, "Any subject if you want");
intent.setPackage("com.google.android.gm");
if (intent.resolveActivity(getPackageManager())!=null)
    startActivity(intent);
else
    Toast.makeText(this,"Gmail App is not installed",Toast.LENGTH_SHORT).show();
CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
12

Try this

i have tried to many solutions but finally i got a correct way that works fine for me

try {
    Intent intent = new Intent (Intent.ACTION_VIEW , Uri.parse("mailto:" + "[email protected]"));
    intent.putExtra(Intent.EXTRA_SUBJECT, "your_subject");
    intent.putExtra(Intent.EXTRA_TEXT, "your_text");
    startActivity(intent);
} catch(Exception e) {
    Toast.makeText(Share.this, "Sorry...You don't have any mail app", Toast.LENGTH_SHORT).show();
    e.printStackTrace(); 
}

Note

  • This will open your installed mail application(Email,Gmail) to send mail in which you can select one among them.
  • Don't use direct package name like("com.google.android.gm") because in future if they change package name your app will face problems.
Dan
  • 3,879
  • 5
  • 36
  • 50
Sunil
  • 3,785
  • 1
  • 32
  • 43
12

Later the requirements changed to starting an "Email app", so the below code basically starts an email app and the user has to choose among the choices shown up.

So, I had to use

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("plain/text");
intent.putExtra(Intent.EXTRA_SUBJECT, "Emailing link");
intent.putExtra(Intent.EXTRA_TEXT, "Link is \n" +
        "This is the body of hte message");
startActivity(Intent.createChooser(intent, ""));
Tim S. Van Haren
  • 8,861
  • 2
  • 30
  • 34
Sana
  • 9,895
  • 15
  • 59
  • 87
  • If you see clearly what droid_fan has answered then it launches email app only on particular devices but my answer launches the email app no matter what the platform is. – Sana Sep 18 '11 at 01:29
  • +1 for the alternative. Having looked again perhaps Richard Lalancette's answer provides a more generic solution for launching packages with unknown launch intent details. – Moog Sep 18 '11 at 02:03
  • This will also show things like PayPal in the intent picker. May not be what you want users to see. – zyamys Sep 09 '17 at 16:59
8

This trick work for ALL version (API 3+), as well as text/plain or text/html (by sonida) :

Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setType("text/html");
// intent.setType("text/plain");
final PackageManager pm = getPackageManager();
final List<ResolveInfo> matches = pm.queryIntentActivities(intent, 0);
ResolveInfo best = null;
for (final ResolveInfo info : matches) {
    if (info.activityInfo.packageName.endsWith(".gm") || info.activityInfo.name.toLowerCase().contains("gmail")) {
        best = info;
        break;
    }
}
if (best != null) {
    intent.setClassName(best.activityInfo.packageName, best.activityInfo.name);
}
intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "YOUR SUBJECT");
intent.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml("YOUR EXTRAS"));

startActivity(intent);
Community
  • 1
  • 1
StephaneT
  • 479
  • 5
  • 11
6

It works.

Intent intent = new Intent(Intent.ACTION_SEND);

String[] strTo = { getString(R.string.mailto) };

intent.putExtra(Intent.EXTRA_EMAIL, strTo);
intent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.mail_subject));
intent.putExtra(Intent.EXTRA_TEXT, getString(R.string.mail_body));

Uri attachments = Uri.parse(image_path);
intent.putExtra(Intent.EXTRA_STREAM, attachments);

intent.setType("message/rfc822");

intent.setPackage("com.google.android.gm");

startActivity(intent);
Masanori Yono
  • 69
  • 1
  • 2
3

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.

Junaid Khan
  • 520
  • 6
  • 15
2

Please check the following code it'll open default mail composer automatically.

try {
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_APP_EMAIL);
        context.startActivity(intent);
} catch (android.content.ActivityNotFoundException anfe) {
        handleException();
}
Mr.Q
  • 29
  • 2
2
startActivity(new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:"+mailId)));
Kishore Reddy
  • 2,394
  • 1
  • 19
  • 15
1

Yes its working code perfectly.....

            Intent intent = new Intent(Intent.ACTION_SEND);
            String[] strTo = { "[email protected]" };
            intent.putExtra(Intent.EXTRA_EMAIL, strTo);
            intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
            intent.putExtra(Intent.EXTRA_TEXT, "Body");
            intent.setType("message/rfc822");
            intent.setPackage("com.google.android.gm");
            startActivity(intent);
priyanka
  • 171
  • 2
  • 12
  • it's 2023 and Android changed `getPackageManager().queryIntentActivities()` to no longer return the GMail package. But priyanka's code still works because the GMail package still has the literal package name `com.google.android.gm`. – Phlip Feb 15 '23 at 22:50
1

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)
        }
Serg Burlaka
  • 2,351
  • 24
  • 35
1

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)
            }
Andrii Artamonov
  • 622
  • 8
  • 15
1

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)
    }
}
touhid udoy
  • 4,005
  • 2
  • 18
  • 31
0

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

Srajan Soni
  • 86
  • 3
  • 12
0
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[] {"[email protected]"});
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;
    }
}
iJustin254
  • 62
  • 6
0

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);
BSMP
  • 4,596
  • 8
  • 33
  • 44