Lukap
Lukap

Reputation: 31963

Send email via gmail

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

Upvotes: 27

Views: 36085

Answers (6)

Ram Bhawan Kushwaha
Ram Bhawan Kushwaha

Reputation: 67

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

Upvotes: -1

marcwjj
marcwjj

Reputation: 1745

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

Upvotes: 8

xbakesx
xbakesx

Reputation: 13500

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

Upvotes: 15

thanhbinh84
thanhbinh84

Reputation: 18454

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.

Upvotes: 21

Igor Popov
Igor Popov

Reputation: 10113

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

Upvotes: 88

Andrew Podkin
Andrew Podkin

Reputation: 165

Replace

i.setType("text/plain");

with

// need this to prompts email client only
i.setType("message/rfc822");

Upvotes: 6

Related Questions