Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send Email Intent

Intent intent = new Intent(Intent.ACTION_SEND); intent.setType("text/html"); intent.putExtra(Intent.EXTRA_EMAIL, "[email protected]"); intent.putExtra(Intent.EXTRA_SUBJECT, "Subject"); intent.putExtra(Intent.EXTRA_TEXT, "I'm email body.");  startActivity(Intent.createChooser(intent, "Send Email")); 

The above code opens a dialog showing following apps:- Bluetooth, Google Docs, Yahoo Mail, Gmail, Orkut, Skype etc.

Actually, I want to filter these list-options. I want to show only email related apps e.g. Gmail, Yahoo Mail. How to do it?

I've seen such example on 'Android Market' application.

  1. Open Android Market app
  2. Open any application where developer has specified his/her email address. (If you can't find such app just open my app:- market://details?id=com.becomputer06.vehicle.diary.free , OR search by 'Vehicle Diary')
  3. Scroll down to 'DEVELOPER'
  4. Click on 'Send Email'

The dialog shows only email Apps e.g. Gmail, Yahoo Mail etc. It does not show Bluetooth, Orkut etc. What code produces such dialog?

like image 803
dira Avatar asked Jan 02 '12 13:01

dira


People also ask

How do I send an Intent?

When you construct an intent, you must specify the action you want the intent to perform. Android uses the action ACTION_SEND to send data from one activity to another, even across process boundaries. You need to specify the data and its type.

What is Intent message?

An intent is to perform an action on the screen. It is mostly used to start activity, send broadcast receiver,start services and send message between two activities. There are two intents available in android as Implicit Intents and Explicit Intents. Here is a sample example to start new activity with old activity.


2 Answers

UPDATE

Official approach:

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

Ref link

OLD ANSWER

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, "Subject"); emailIntent.putExtra(Intent.EXTRA_TEXT, "Body"); startActivity(Intent.createChooser(emailIntent, "Send email...")); 

Update: According to marcwjj, it seems that on 4.3, we need to pass string array instead of a string for email address to make it work. We might need to add one more line:

intent.putExtra(Intent.EXTRA_EMAIL, addresses); // String[] addresses 
like image 97
thanhbinh84 Avatar answered Oct 03 '22 00:10

thanhbinh84


There are three main approaches:

String email = /* Your email address here */ String subject = /* Your subject here */ String body = /* Your body here */ String chooserTitle = /* Your chooser title here */ 

1. Custom Uri:

Uri uri = Uri.parse("mailto:" + email)     .buildUpon()     .appendQueryParameter("subject", subject)     .appendQueryParameter("body", body)     .build();  Intent emailIntent = new Intent(Intent.ACTION_SENDTO, uri); startActivity(Intent.createChooser(emailIntent, chooserTitle)); 

2. Using Intent extras:

Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:" + email)); emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject); emailIntent.putExtra(Intent.EXTRA_TEXT, body); //emailIntent.putExtra(Intent.EXTRA_HTML_TEXT, body); //If you are using HTML in your body text  startActivity(Intent.createChooser(emailIntent, "Chooser Title")); 

3. Support Library ShareCompat:

Activity activity = /* Your activity here */  ShareCompat.IntentBuilder.from(activity)     .setType("message/rfc822")     .addEmailTo(email)     .setSubject(subject)     .setText(body)     //.setHtmlText(body) //If you are using HTML in your body text     .setChooserTitle(chooserTitle)     .startChooser(); 
like image 23
dira Avatar answered Oct 02 '22 23:10

dira