26

I'm trying to link a button to the mail app. Not to send mail, but just to open the inbox.

Should I do this with Intent intent = new Intent(...)?

If so, what should be between the ( )?

makes
  • 6,438
  • 3
  • 40
  • 58
Sander Swart
  • 261
  • 1
  • 3
  • 3

15 Answers15

42

If the goal is to open the default email app to view the inbox, then key is to add an intent category and use the ACTION_MAIN intent like so:

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_APP_EMAIL);
getActivity().startActivity(intent);

https://developer.android.com/reference/android/content/Intent.html#CATEGORY_APP_EMAIL

Zoe
  • 27,060
  • 21
  • 118
  • 148
Ben Yee
  • 1,547
  • 13
  • 19
  • how would i mimic this in react native? – jasan Jun 18 '17 at 19:49
  • @jasan - you would need to create a custom component to trigger this logic from js land – Ben Yee Jun 20 '17 at 04:39
  • 18
    friendly reminder: this will cause `email` Activity to open **IN** your current stack (ie. in your app), causing bad user exp. To open `email` in new window, add line `intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)` before start it. – Samuel T. Chou Oct 28 '20 at 06:32
8

This code worked for me. It opens a picker with all email apps registered to device and straight to Inbox:

Intent emailIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("mailto:"));
    PackageManager pm = getPackageManager();

    List<ResolveInfo> resInfo = pm.queryIntentActivities(emailIntent, 0);
    if (resInfo.size() > 0) {
        ResolveInfo ri = resInfo.get(0);
        // First create an intent with only the package name of the first registered email app
        // and build a picked based on it
        Intent intentChooser = pm.getLaunchIntentForPackage(ri.activityInfo.packageName);
        Intent openInChooser =
                Intent.createChooser(intentChooser,
                        getString(R.string.user_reg_email_client_chooser_title));

        // Then create a list of LabeledIntent for the rest of the registered email apps 
        List<LabeledIntent> intentList = new ArrayList<LabeledIntent>();
        for (int i = 1; i < resInfo.size(); i++) {
            // Extract the label and repackage it in a LabeledIntent
            ri = resInfo.get(i);
            String packageName = ri.activityInfo.packageName;
            Intent intent = pm.getLaunchIntentForPackage(packageName);
            intentList.add(new LabeledIntent(intent, packageName, ri.loadLabel(pm), ri.icon));
        }

        LabeledIntent[] extraIntents = intentList.toArray(new LabeledIntent[intentList.size()]);
        // Add the rest of the email apps to the picker selection
        openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents);
        startActivity(openInChooser);
    }
Larisa Hogas
  • 271
  • 3
  • 3
  • 1
    This crashes when the launch intent can't be resolved – needs another check for that! – Hannes Struß May 25 '16 at 13:45
  • This cannot work properly on android > 11. since queryIntentActivities is going to return only the packages on the device that has been listed in the manifest in query blocks. (We could add a limited known sets of mail app but that sucks ...) – Timo Sep 03 '21 at 09:32
7

Based on the answer https://stackoverflow.com/a/28190156/3289338

Starting from Android 11 the system won't return anything for queryIntentActivities because we first need to add an entry in the queries (in the manifest) like this

<manifest ...>

<queries>
    ...
    <intent>
      <action
        android:name="android.intent.action.VIEW" />
      <data
        android:scheme="mailto" />
    </intent>
</queries>

...

</manifest>

and here a kotlin version of the solution:

fun Context.openMailbox(chooserTitle: String) {
    val emailIntent = Intent(Intent.ACTION_VIEW, Uri.parse("mailto:"))

    val resInfo = packageManager.queryIntentActivities(emailIntent, 0)
    if (resInfo.isNotEmpty()) {
        // First create an intent with only the package name of the first registered email app
        // and build a picked based on it
        val intentChooser = packageManager.getLaunchIntentForPackage(
            resInfo.first().activityInfo.packageName
        )
        val openInChooser = Intent.createChooser(intentChooser, chooserTitle)

        // Then create a list of LabeledIntent for the rest of the registered email apps
        val emailApps = resInfo.toLabeledIntentArray(packageManager)

        // Add the rest of the email apps to the picker selection
        openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, emailApps)
        startActivity(openInChooser)
    } else {
        Timber.e("No email app found")
    }
}

private fun List<ResolveInfo>.toLabeledIntentArray(packageManager: PackageManager): Array<LabeledIntent> = map {
    val packageName = it.activityInfo.packageName
    val intent = packageManager.getLaunchIntentForPackage(packageName)
    LabeledIntent(intent, packageName, it.loadLabel(packageManager), it.icon)
}.toTypedArray()
Timo
  • 497
  • 10
  • 21
  • I am getting only 3 apps in the chooser, although I double checked that the list has 5 elements, but I only see 3, did you face something similar? – Sca09 May 09 '22 at 07:24
  • If you mean in emailApps variable there are 5 elements and it only display 3, then no. It works fine for me for quite a while even on the new version of android. – Timo May 11 '22 at 09:41
6

To open it new task use the below code:

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_APP_EMAIL);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Fakhriddin Abdullaev
  • 4,169
  • 2
  • 35
  • 37
6

Any suggestions to avoid the crash if the default mail in the device is not configured?

Yes, it's possible to open the Android default email inbox.
Use this code:

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


This code works, you have to configure your Android device default mail first. If you already configured your mail it works fine. Otherwise, it force closes with a NullPointerException.

Community
  • 1
  • 1
Mayur Bhola
  • 735
  • 5
  • 17
  • 6
    It's WRONG: The solution assumes that the user has an app with the "com.android.email" package and that it's the default email app they use. If I have, for example, K9 mail and I use that as my default client, opening the stock mail client is not useful to me. The intent wasn't to make fun, there's this thing called "internet etiquette". When you type in all caps it is considered to be SHOUTING and rude. Aside from that it makes your text look obnoxious and hard to read. There is a reason why there are capital and small letters. Properly formatted text is much more readable than ALL CAPS. – copolii Jan 16 '12 at 18:54
  • 3
    yes that's good that you find my fault and i don't perfectly this question and gives wrong answer ,but you have to write this comment first time instead you write that "are the caps really necessary?" if you know the right answer plz give me.. bcoz i knew about only this answer as i given above for the same question like "How to open the default mail inbox from android code?"..! – Mayur Bhola Jan 17 '12 at 11:36
3

You can simply use below code when for no attachment:

Intent i = new Intent(Intent.ACTION_SENDTO);
i.setData(Uri.parse("mailto:support@mailname.com")); 
i.putExtra(Intent.EXTRA_SUBJECT, "Feedback/Support");
startActivity(Intent.createChooser(emailIntent, "Send feedback"));

For details I recommend to visit: https://developer.android.com/guide/components/intents-common.html#Email

Monir Zzaman
  • 459
  • 4
  • 7
2

I'm with jetpack compose and this works for me :

val context = LocalContext.current

val intent = Intent(Intent.ACTION_MAIN)

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
intent.addCategory(Intent.CATEGORY_APP_EMAIL)

Button(
onClick = { startActivity(context, intent, null) }
)
lerey
  • 39
  • 6
1

For kotlin:

fun composeEmail(addresses: Array<String>, subject: String) {
    val intent = Intent(Intent.ACTION_SENDTO).apply {
        data = Uri.parse("mailto:") // only email apps should handle this
        putExtra(Intent.EXTRA_EMAIL, addresses)
        putExtra(Intent.EXTRA_SUBJECT, subject)
    }
    if (intent.resolveActivity(packageManager) != null) {
        startActivity(intent)
    }
}

Ref: https://developer.android.com/reference/android/content/Intent.html#CATEGORY_APP_EMAIL

Jamil Hasnine Tamim
  • 4,389
  • 27
  • 43
1
val intent = Intent(Intent.ACTION_MAIN)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
intent.addCategory(Intent.CATEGORY_APP_EMAIL)
startActivity(intent)
1

The code works for me:

Intent intent= new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_APP_EMAIL);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(Intent.createChooser(intent, ""));
Anh Duy
  • 1,145
  • 15
  • 25
1
  You can use this but it is for gmail only

  Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);  
  emailIntent.setType("plain/text");
  startActivity(emailIntent); 
  • sorry not that one but try using this one: `Intent mailClient = new Intent(Intent.ACTION_VIEW); mailClient.setClassName("com.google.android.gm", "com.google.android.gm.ConversationListActivity"); startActivity(mailClient);` – Alex Cooper Nov 12 '11 at 05:42
  • tnx for your response. I just realized I'm trying to reach the HTC mail app, not the default mail app. Is there a way to open that one in the inbox? or can you show me where i can find that information? – Sander Swart Nov 13 '11 at 13:39
  • how do I pass the subject of the mail and also the TO value? – TharakaNirmana Oct 09 '13 at 09:28
0
Intent email = new Intent(Intent.ACTION_MAIN);
              
email.addCategory(Intent.CATEGORY_APP_EMAIL);
                    startActivity(email);
Ryan M
  • 18,333
  • 31
  • 67
  • 74
sara
  • 1
0

Bit late, here is proper working code.

Intent intent = Intent.makeMainSelectorActivity(
Intent.ACTION_MAIN,
Intent.CATEGORY_APP_EMAIL
);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(Intent.createChooser(intent, "Email"));

For further details check this document:

  1. CATEGORY_APP_EMAIL
  2. makeMainSelectorActivity
Mohammad Imran
  • 3,264
  • 4
  • 23
  • 37
0

Unfortunately it doesn't look promising. This has been asked before

How do I launch the email client directly to inbox view?

you can open the email client in compose mode, but you seem to already know that.

Community
  • 1
  • 1
skynet
  • 9,898
  • 5
  • 43
  • 52
-2

You can open Android default e-mail client using this:

Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("text/plain");
emailIntent.setClassName("com.android.email", "com.android.email.activity.Welcome");
emailIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(emailIntent);
Alysson Myller
  • 1,203
  • 11
  • 13