Send email via gmail

Lukap picture Lukap · Nov 27, 2011 · Viewed 34.9k times · Source

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

Answer

Igor Popov picture Igor Popov · Nov 27, 2011

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