3

I have found various topics here and elsewhere on creating an intent for sending e-mail and that seems to be pretty straightforward. I'm looking for an intent to just launch any e-mail client the user might have.

Here is the code I've seen for sending an e-mail (posted just for reference, this doesn't serve my needs as I don't want to send a new message):

Intent i = new Intent(Intent.ACTION_SEND); 
i.setType("text/plain"); 
i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"recipient@example.com"}); 
i.putExtra(Intent.EXTRA_SUBJECT, "Subject of the message"); 
i.putExtra(Intent.EXTRA_TEXT   , "Body of the message"); 

Here is the code I put together for specifically launching the Gmail client by package name:

PackageManager pm = getPackageManager();
Intent intent = pm.getLaunchIntentForPackage("com.google.android.gm");
startActivity(intent);

The code above works but isn't flexible in that a user might not be using Gmail but the other built-in e-mail application or a 3rd party e-mail app. I'm looking for an intent that would bring up the chooser in this case so the user can decide which app to launch to read e-mail.

Does anyone know how to accomplish this?

Charles
  • 50,943
  • 13
  • 104
  • 142
afonseca
  • 817
  • 2
  • 7
  • 10

3 Answers3

5

Can a mailto URL be used in some fashion to accomplish this? --Edit-- I was able to accomplish this using the following code sample:

mt = MailTo.parse("mailto:yourname@gmail.com");
sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{mt.getTo()});
sendIntent.putExtra(Intent.EXTRA_TEXT, "");
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Enter a subject");
sendIntent.setType("message/rfc822");
startActivity(Intent.createChooser(sendIntent, "Send a Message:"));
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
Fred
  • 402
  • 4
  • 9
  • This will send an email. This is not what the OP wanted. The OP said, in boldface, "I don't want to send a new message". – CommonsWare Jan 06 '16 at 14:37
1

Another approach could be Intent.createChooser(); and let the user to choose the right application.

BTW The list could contain not only email applications

Francesco Laurita
  • 23,434
  • 8
  • 55
  • 63
  • Thanks for the alternate approach but this won't work in my situation because I don't want the user to have to choose every time. – afonseca Jul 01 '10 at 07:39
0

Does anyone know how to accomplish this?

There is no such Intent -- you can tell this by examining the manifest for the Email application.

The only thing you can do is build yourself a list of email clients you wish to link to and use the PackageManager code you show above for each.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks, looks like this will be the way I have to go. You'd think this would be a common type of intent that would be included as part of the platform. – afonseca Jul 01 '10 at 07:38
  • Can you tell me how to build myself a list of email clients and use packmanager with it? – Bhargav Jan 06 '16 at 14:34
  • @Bhargav: You are certainly welcome to ask a separate Stack Overflow question about this. – CommonsWare Jan 06 '16 at 14:36
  • @CommonsWare done http://stackoverflow.com/questions/34635746/how-to-create-a-chooser-for-a-list-of-applications-using-package-manager – Bhargav Jan 06 '16 at 14:39