6

I need to check whether email client is installed on a device or not. I have used the following code but it does not work for me.

public boolean isIntentAvailable() {
    final PackageManager packageManager = getApplicationContext().getPackageManager();
    final Intent intent = new Intent(Intent.ACTION_SEND);
    intent.putExtra(Intent.EXTRA_EMAIL, "vin@gmail.com");
    List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.GET_META_DATA);
    return list.size() > 0;
} 
harshit
  • 3,448
  • 3
  • 23
  • 54
user1503346
  • 71
  • 1
  • 5

3 Answers3

13

Use this, works for me :

public static boolean isMailClientPresent(Context context){
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("text/html");
    final PackageManager packageManager = context.getPackageManager();
    List<ResolveInfo> list = packageManager.queryIntentActivities(intent, 0);

    if(list.size() == 0)
        return false;
    else 
        return true;
}
Nitin
  • 1,415
  • 13
  • 16
  • 1
    This isn't exactly full proof. If you run this using the type as "text/html", it can still find other means that would work other than email. For example, I setup a new emulator and when attempting to call this it shows my list of ResolveInfo objects containing an item via Bluetooth. So, this function call returns true with no email client available. I would recommend that you update this and change the type to "message/rfc822" just as @eyal suggested. – cking24343 May 19 '17 at 11:58
  • In my case in `emulator` without Google API support it returns a list with one element (activity "com.android.fallback.Fallback"), then raises an exception when sending an email. See https://stackoverflow.com/a/31052350/2914140 for this case. – CoolMind Apr 18 '18 at 17:48
3

For email client, specifically, you should use:

intent.setType("message/rfc822");

instead of:

intent.setType("text/html");
eyal
  • 2,251
  • 7
  • 40
  • 51
  • "message/rfc822" results in queryIntentActivities() returning a list.size() of 1 after I delete gmail. Is there a bulletproof solution to check for installed email client? – Ed of the Mountain Apr 12 '18 at 16:16
  • 1
    @EdoftheMountain thats sounds coorect, most devices has defualt email client AND gmail. so after you deleted gmail you probably left with defaul one – Yarh Jun 11 '18 at 08:29
1

use this method:

    private fun sendEmail(to: Array<String>) {
        val intent = Intent(Intent.ACTION_SENDTO)
        intent.data = Uri.parse("mailto:") // only email apps should handle this
        intent.putExtra(Intent.EXTRA_EMAIL, to)
//        intent.putExtra(Intent.EXTRA_SUBJECT, subject)
        if (intent.resolveActivity(requireContext().packageManager) != null) {
            startActivity(intent)
        }
    }

To be able to check for email clients when targeting api 30, add "queries" to the Manifest:

<queries>
    <intent>
        <action android:name="android.intent.action.SENDTO" />
        <data android:scheme="*" />
    </intent>
</queries>
fullmoon
  • 6,463
  • 3
  • 34
  • 52