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

royhowie
  • 11,075
  • 14
  • 50
  • 67
Lukap
  • 31,523
  • 64
  • 157
  • 244

6 Answers6

87

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@gmail.com") + 
          "?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..."));
xbakesx
  • 13,202
  • 6
  • 48
  • 76
Igor Popov
  • 9,795
  • 7
  • 55
  • 68
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","abc@gmail.com", null));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "EXTRA_SUBJECT");
startActivity(Intent.createChooser(emailIntent, "Send email..."));

Hope this helps.

thanhbinh84
  • 17,876
  • 6
  • 62
  • 69
  • 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. – Giulio Piancastelli Aug 02 '13 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. – thanhbinh84 Aug 07 '13 at 04: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 Feb 16 '14 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");
}
xbakesx
  • 13,202
  • 6
  • 48
  • 76
  • 1
    Will try to use it in my app. But last line - chooser -> createChooser – WindRider Apr 30 '13 at 11:38
  • I'll update my answer, there's no method `chooser`, it's `createChooser()`. – xbakesx Apr 30 '13 at 14:07
  • What if I want to add an attachment? – cmoaciopm Sep 11 '13 at 08: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): http://stackoverflow.com/questions/5401104/android-exporting-to-csv-and-sending-as-email-attachment/11471489#11471489. 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 Sep 11 '13 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);
        }
    }
marcwjj
  • 1,705
  • 1
  • 12
  • 11
6

Replace

i.setType("text/plain");

with

// need this to prompts email client only
i.setType("message/rfc822");
Andrew Podkin
  • 165
  • 1
  • 1
  • 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. – Paul Lammertsma May 28 '13 at 10:02
  • 1
    Yeah but mailto: doesn't support attachments. – Ed Burnette Jun 14 '13 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 Sep 11 '13 at 08:58
  • Multiple attachments crashes Gmail with mailto:, unfortunately. :( – Learn OpenGL ES Mar 10 '14 at 18:25
  • Figured out how to do it with multiple attachments: http://stackoverflow.com/questions/22240028/opening-an-email-with-multiple-attachments-while-restricting-the-chooser-to-onl – Learn OpenGL ES Mar 10 '14 at 20:17
-1
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
                    "mailto","opinions@gmail.com.com", 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);
SachinSarawgi
  • 2,632
  • 20
  • 28