Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 730
Lukap Avatar asked Nov 27 '11 09:11

Lukap


People also ask

Can I send email from Gmail to another email?

On your computer, open Gmail. See all settings. Click the Accounts and import or Accounts tab. In the "Send mail as" section, click Add another email address.


2 Answers

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...")); 
like image 123
Igor Popov Avatar answered Sep 21 '22 00:09

Igor Popov


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.

like image 34
thanhbinh84 Avatar answered Sep 17 '22 00:09

thanhbinh84