27

I have a code the fires intent for sending email

Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_EMAIL,
                new String[] { to });
i.putExtra(Intent.EXTRA_SUBJECT, subject);
i.putExtra(Intent.EXTRA_TEXT, msg);
try {
    startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
    Toast.makeText(Start.this,
                    "There are no email clients installed.",
                    Toast.LENGTH_SHORT).show();
}

But when this intent is fired I see many item in the list like sms app , gmail app, facebook app and so on.

How can I filter this and enable only gmail app (or maybe just email apps)?

6 Answers 6

88

Use android.content.Intent.ACTION_SENDTO (new Intent(Intent.ACTION_SENDTO);) to get only the list of e-mail clients, with no facebook or other apps. Just the email clients.

I wouldn't suggest you get directly to the email app. Let the user choose his favorite email app. Don't constrain him.

If you use ACTION_SENDTO, putExtra does not work to add subject and text to the intent. Use Uri to add the subject and body text.

Example

Intent send = new Intent(Intent.ACTION_SENDTO);
String uriText = "mailto:" + Uri.encode("[email protected]") + 
          "?subject=" + Uri.encode("the subject") + 
          "&body=" + Uri.encode("the body of the message");
Uri uri = Uri.parse(uriText);

send.setData(uri);
startActivity(Intent.createChooser(send, "Send mail..."));
4
  • Could you please tell me how to add an image to this? I tried &attach=(imagefilepath). But, it doesn't seem to work!
    – Abhijit
    Commented Jan 19, 2012 at 21:51
  • 7
    The intent chooser says no apps installed to perform this intent when I use ACTION_SENDTO. I'm using Android 4.1.2 and I have an email app installed...
    – ffleandro
    Commented Nov 28, 2012 at 12:06
  • i m getting the same msg when i change from ACTION_SENT to ACTION_SENDTO
    – iRunner
    Commented Feb 23, 2013 at 9:23
  • Works like a charm in Android 6.0.1. Commented Mar 29, 2016 at 14:42
21

The accepted answer doesn't work on the 4.1.2. This should work on all platforms:

Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
            "mailto","[email protected]", null));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "EXTRA_SUBJECT");
startActivity(Intent.createChooser(emailIntent, "Send email..."));

Hope this helps.

3
  • 1
    If you don't know the recipient's address, you can't build the Uri with fromParts, but you have to use Uri.parse("mailto:") and pass it to Intent.setData. Then, if you have some body text besides the subject, you may use Intent.EXTRA_TEXT to pass that with the intent. Commented Aug 2, 2013 at 16:31
  • 2
    If you don't know the recipient's address, just put an empty string there. I've just checked and see it works normally. Commented Aug 7, 2013 at 4:31
  • 2
    The PayPal app will appear in the chooser dialog if you do this, which seems I just want to steal my costumer some money instead of retrieving a feedback :S Note that this happens only from the last update of the PayPal app (a week or so ago) Can we prevent this from happening?
    – doplumi
    Commented Feb 16, 2014 at 10:02
15

Igor Popov's answer is 100% correct, but in case you want a fallback option, I use this method:

public static Intent createEmailIntent(final String toEmail, 
                                       final String subject, 
                                       final String message)
{
    Intent sendTo = new Intent(Intent.ACTION_SENDTO);
    String uriText = "mailto:" + Uri.encode(toEmail) +
            "?subject=" + Uri.encode(subject) +
            "&body=" + Uri.encode(message);
    Uri uri = Uri.parse(uriText);
    sendTo.setData(uri);

    List<ResolveInfo> resolveInfos = 
        getPackageManager().queryIntentActivities(sendTo, 0);

    // Emulators may not like this check...
    if (!resolveInfos.isEmpty())
    {
        return sendTo;
    }

    // Nothing resolves send to, so fallback to send...
    Intent send = new Intent(Intent.ACTION_SEND);

    send.setType("text/plain");
    send.putExtra(Intent.EXTRA_EMAIL,
               new String[] { toEmail });
    send.putExtra(Intent.EXTRA_SUBJECT, subject);
    send.putExtra(Intent.EXTRA_TEXT, message);

    return Intent.createChooser(send, "Your Title Here");
}
4
  • 1
    Will try to use it in my app. But last line - chooser -> createChooser
    – WindRider
    Commented Apr 30, 2013 at 11:38
  • I'll update my answer, there's no method chooser, it's createChooser().
    – xbakesx
    Commented Apr 30, 2013 at 14:07
  • What if I want to add an attachment?
    – cmoaciopm
    Commented Sep 11, 2013 at 8:54
  • There are two solutions, if you just have one you can use Intent.EXTRA_STREAM as you can see here (although I don't know what the support for this is within different mail apps): stackoverflow.com/questions/5401104/…. That will only work if you have one attachment. If you have more than one I think you'll have to base64 the attachment and put it in the body, then link to it from the content.
    – xbakesx
    Commented Sep 11, 2013 at 12:59
8

This is quoted from Android official doc, I've tested it on Android 4.4, and works perfectly. See more examples at https://developer.android.com/guide/components/intents-common.html#Email

  public void composeEmail(String[] addresses, String subject) {
        Intent intent = new Intent(Intent.ACTION_SENDTO);
        intent.setData(Uri.parse("mailto:")); // only email apps should handle this
        intent.putExtra(Intent.EXTRA_EMAIL, addresses);
        intent.putExtra(Intent.EXTRA_SUBJECT, subject);
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivity(intent);
        }
    }
1
  • this doesnt help with attachments thought
    – AmaJayJB
    Commented Sep 1, 2015 at 9:24
6

Replace

i.setType("text/plain");

with

// need this to prompts email client only
i.setType("message/rfc822");
5
  • 1
    Using the MIME type to perform a send operation is a bad idea, because you're basically instructing Android to provide a list of apps that support sending a file of type message/rfc822. That's not the same as sending an e-mail. Use the mailto: protocol instead, because that's what e-mail clients actually understand. Commented May 28, 2013 at 10:02
  • 1
    Yeah but mailto: doesn't support attachments. Commented Jun 14, 2013 at 16:43
  • @EdBurnette In fact, whether "mailto:" support attachment depends on mail app. For example, the latest "K9 Mail" supports using mailto with attachment.
    – cmoaciopm
    Commented Sep 11, 2013 at 8:58
  • Multiple attachments crashes Gmail with mailto:, unfortunately. :( Commented Mar 10, 2014 at 18:25
  • Figured out how to do it with multiple attachments: stackoverflow.com/questions/22240028/… Commented Mar 10, 2014 at 20:17
-1
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
                    "mailto","[email protected]", null));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "IndiaTV News - Mobile App Feedback");
emailIntent.putExtra(Intent.EXTRA_TEXT,Html.fromHtml(Settings.this.getString(R.string.MailContent)));
startActivityForResult(Intent.createChooser(emailIntent, "Send email..."),0);

Not the answer you're looking for? Browse other questions tagged or ask your own question.