简体   繁体   中英

How to limit the email intent in android app?

I am using this code to use email application from my app.

String mailText = "Full Name:" + fname.getText().toString();
String subject = "Support";
Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_EMAIL, new String[] { "support@roncocala.com" });

email.putExtra(Intent.EXTRA_SUBJECT, subject);
email.setType("plain/text");

email.putExtra(Intent.EXTRA_TEXT, mailText);

startActivity(Intent.createChooser(email, "Choose an Email client :"));

But it shows extra applications like SKype and ES File Lan . Is there a way to limit these application to mail applications like gmail,yahoo,hotmail. Please help.Thanks.

To get only email client you need to use android.content.Intent.ACTION_SENDTO :

new Intent(Intent.ACTION_SENDTO); // return only the list of e-mail clients

you need to have configured an email account on those email client app or you'll have the error : "No application can perform this action".

ACTION_SENDTO only seems to be working for newer OS (at least API LEVEL 17+).

Unfortunately, this is the "best" current way of limiting the application list if you want to support older Android OS.

emailIntent.setType("message/rfc822");

None of the above solutions worked for me. After a lot of searching and testing, I finally found a good solution. Thanks to the Open source developer, cketti for sharing his/her concise and neat solution.

String mailto = "mailto:bob@example.org" +
    "?cc=" + "alice@example.com" +
    "&subject=" + Uri.encode(subject) +
    "&body=" + Uri.encode(bodyText);

Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
emailIntent.setData(Uri.parse(mailto));

try {
  startActivity(emailIntent);
} catch (ActivityNotFoundException e) {
  //TODO: Handle case where no email app is available
}

And this is the link to his/her gist.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM