Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open Email program via Intents (but only an Email program)

Tags:

android

I want to setup a part of my application that allows users to send a quick email to another user. It's not very hard to set this up:

Intent intent = new Intent(Intent.ACTION_SEND); intent.setType("text/plain"); intent.putExtra(Intent.EXTRA_SUBJECT, subject); intent.putExtra(Intent.EXTRA_TEXT, message); Intent mailer = Intent.createChooser(intent, null); startActivity(mailer); 

However, the problem is that the ACTION_SEND is accepted by more than just email programs - for example, on my phone the Facebook app, Twitter, reddit is fun, and even Bluetooth come up as viable alternatives for sending this message. The message is entirely too long for some of these (especially Twitter).

Is there a way to limit the chooser to just applications that support long messages (such as email)? Or is there a way to detect the app that the user has chosen and adjust the message appropriately?

like image 356
Dan Lew Avatar asked Jul 22 '10 19:07

Dan Lew


People also ask

How do I open my email app?

From the Home screen, tap the Apps icon (in the QuickTap bar) > the Apps tab (if necessary) > Email or tap the Email icon directly from the Home screen. The first time you open the Email app, a setup wizard opens to help you add an email account.

How do I open email on Android?

Go to Settings > Add account > Other. Enter your full email address, such as [email protected] and then tap Manual Setup. Choose Personal (IMAP) or Personal (POP3). Enter your password and tap Next.

What is intent email?

A letter of intent is an introductory letter to employers you're interested in working for. Typically, you would send a letter of intent to hiring managers or recruiters at a company that has not posted jobs relevant to your background.


2 Answers

Thanks to Pentium10's suggestion of searching how Linkify works, I have found a great solution to this problem. Basically, you just create a "mailto:" link, and then call the appropriate Intent for that.:

Intent intent = new Intent(Intent.ACTION_VIEW); Uri data = Uri.parse("mailto:?subject=" + subject + "&body=" + body); intent.setData(data); startActivity(intent); 

There are a few interesting aspects to this solution:

  1. I'm using the ACTION_VIEW action because that's more appropriate for a "mailto:" link. You could provide no particular action, but then you might get some unsatisfactory results (for example, it will ask you if you want to add the link to your contacts).

  2. Since this is a "share" link, I am simply including no email address - even though this is a mailto link. It works.

  3. There's no chooser involved. The reason for this is to let the user take advantage of defaults; if they have set a default email program, then it'll take them straight to that, bypassing the chooser altogether (which seems good in my mind, you may argue otherwise).

Of course there's a lot of finesse I'm leaving out (such as properly encoding the subject/body), but you should be able to figure that out on your own.

like image 52
Dan Lew Avatar answered Sep 20 '22 03:09

Dan Lew


Changing the MIME type is the answer, this is what I did in my app to change the same behavior. I used intent.setType("message/rfc822");

like image 35
Jeff S Avatar answered Sep 24 '22 03:09

Jeff S