Sander Swart
Sander Swart

Reputation: 271

How to open the default mail inbox from android code?

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

Upvotes: 27

Views: 26497

Answers (16)

Nino van Hooff
Nino van Hooff

Reputation: 3893

2024 edition

fun Context.openDefaultEmailApp(){
    val intent = Intent(Intent.ACTION_MAIN)
    intent.addCategory(Intent.CATEGORY_APP_EMAIL)
    startActivity(intent)
}

Usage:

context.openDefaultEmailApp()

Using a Kotlin extension function of Context which also works with Application because Application is a Context

Upvotes: 0

Anh Duy
Anh Duy

Reputation: 1183

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, ""));

Upvotes: 1

lerey
lerey

Reputation: 51

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) }
)

Upvotes: 2

Bishwajeet Singh
Bishwajeet Singh

Reputation: 96

val intent = Intent(Intent.ACTION_MAIN)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
intent.addCategory(Intent.CATEGORY_APP_EMAIL)
startActivity(intent)

Upvotes: 1

Ben Yee
Ben Yee

Reputation: 1595

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

Upvotes: 42

sara
sara

Reputation: 1

Intent email = new Intent(Intent.ACTION_MAIN);
              
email.addCategory(Intent.CATEGORY_APP_EMAIL);
                    startActivity(email);

Upvotes: 0

Timo
Timo

Reputation: 513

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

Upvotes: 9

Mohammad Imran
Mohammad Imran

Reputation: 3273

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

Upvotes: 0

Jamil Hasnine Tamim
Jamil Hasnine Tamim

Reputation: 4448

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

Upvotes: 1

Fakhriddin Abdullaev
Fakhriddin Abdullaev

Reputation: 4920

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);

Upvotes: 7

Mayur Bhola
Mayur Bhola

Reputation: 735

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.

Upvotes: 6

Monir Zzaman
Monir Zzaman

Reputation: 489

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

Upvotes: 3

Larisa Hogas
Larisa Hogas

Reputation: 271

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);
    }

Upvotes: 8

Alysson Myller
Alysson Myller

Reputation: 1213

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);

Upvotes: -2

Alex Cooper
Alex Cooper

Reputation: 54

  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); 

Upvotes: 1

skynet
skynet

Reputation: 9908

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.

Upvotes: 0

Related Questions