How to open the default mail inbox from android code?
Asked Answered
A

15

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 ( )?

Aureus answered 11/11, 2011 at 19:36 Comment(0)
P
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

Pretty answered 23/7, 2015 at 17:58 Comment(3)
how would i mimic this in react native?Dwarf
@Dwarf - you would need to create a custom component to trigger this logic from js landPretty
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.Average
L
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);
    }
Lombardo answered 28/1, 2015 at 10:40 Comment(2)
This crashes when the launch intent can't be resolved – needs another check for that!Benioff
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 ...)Respite
R
8

Based on the answer https://mcmap.net/q/169539/-how-to-open-the-default-mail-inbox-from-android-code

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()
Respite answered 3/9, 2021 at 10:42 Comment(2)
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?Liquidity
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.Respite
E
7

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);
Edison answered 29/6, 2020 at 3:0 Comment(0)
B
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.

Beaner answered 29/11, 2011 at 9:16 Comment(2)
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.Slumberous
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?"..!Beaner
A
3

You can simply use below code when for no attachment:

Intent i = new Intent(Intent.ACTION_SENDTO);
i.setData(Uri.parse("mailto:[email protected]")); 
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

Amphibrach answered 4/4, 2019 at 4:44 Comment(0)
E
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) }
)
Extortionary answered 10/8, 2022 at 13:40 Comment(0)
B
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); 
Beleaguer answered 12/11, 2011 at 5:39 Comment(3)
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);Beleaguer
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?Aureus
how do I pass the subject of the mail and also the TO value?Kao
A
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

Antelope answered 21/3, 2021 at 8:42 Comment(0)
D
1
val intent = Intent(Intent.ACTION_MAIN)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
intent.addCategory(Intent.CATEGORY_APP_EMAIL)
startActivity(intent)
Dennadennard answered 18/1, 2022 at 19:37 Comment(0)
D
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, ""));
Dutcher answered 29/11, 2022 at 8:55 Comment(0)
K
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.

Kirkkirkcaldy answered 11/11, 2011 at 19:41 Comment(0)
T
0
Intent email = new Intent(Intent.ACTION_MAIN);
              
email.addCategory(Intent.CATEGORY_APP_EMAIL);
                    startActivity(email);
Triangle answered 5/7, 2019 at 17:43 Comment(0)
C
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
Chukchi answered 24/5, 2021 at 12:25 Comment(0)
U
-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);
Ultranationalism answered 29/10, 2012 at 12:54 Comment(1)
this composes a new email, the original question was to just open the default email app.Pretty

© 2022 - 2024 — McMap. All rights reserved.