Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Android Intent.ACTION_SEND for sending email

I'm using Intent.ACTION_SEND to send an email. However, when I call the intent it is showing choices to send a message, send an email, and also to send via bluetooth. I want it to only show choices to send an email. How can I do this?

like image 629
Ganapathy C Avatar asked Feb 03 '11 06:02

Ganapathy C


People also ask

What is Intent action send?

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.

Which type of Intent would you use in your own app to send an email using an external mail app?

Intent Object - Action to send Email You will use ACTION_SEND action to launch an email client installed on your Android device.

How do I open Gmail with Intent?

Use this: Intent intent = getPackageManager(). getLaunchIntentForPackage("com.google.android.gm"); startActivity(intent);


1 Answers

[Solution for API LEVEL >=15]

I've finally succeded in sending email WITH attachments to ONLY email clients. I write it here because it took me a lot of time and it may be usefull to others.

The problem is:

  • Intent.ACTION_SENDTO takes Data URI (so you can specify "mailto:" schema) BUT it does not accept Intent:EXTRA_STREAM.

  • Intent.ACTION_SEND accepts Intent:EXTRA_STREAM (so you can add attachment) BUT it takes only Type (not Data URI so you cannot specify "mailto:" schema).

So Intent.ACTION_SEND lets the user choose from several Activities, even if you setType("message/rfc822"), because that App/Activities can manage all file types (tipically GDrive/Dropbox Apps) and so even email message files.

The solution is in the setSelector method. With this method you can use Intent.ACTION_SENDTO to select the Activity, but then send the Intent.ACTION_SEND Intent.

Here my solution code (the attachment came from a FileProvider, but it could be any file):

{     Intent emailSelectorIntent = new Intent(Intent.ACTION_SENDTO);     emailSelectorIntent.setData(Uri.parse("mailto:"));      final Intent emailIntent = new Intent(Intent.ACTION_SEND);     emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"[email protected]"});     emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");     emailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);     emailIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);     emailIntent.setSelector( emailSelectorIntent );      Uri attachment = FileProvider.getUriForFile(this, "my_fileprovider", myFile);     emailIntent.putExtra(Intent.EXTRA_STREAM, attachment);      if( emailIntent.resolveActivity(getPackageManager()) != null )         startActivity(emailIntent); } 
like image 193
ARLabs Avatar answered Oct 04 '22 22:10

ARLabs