Home » Android » android – How to send Email with attachment using only email apps?

android – How to send Email with attachment using only email apps?

Posted by: admin May 14, 2020 Leave a comment


There are two requirements:

  • Email with attachment
  • In the Intent chooser, there should be only Email apps.

What I have known/done:

  • Intent.ACTION_SENDTO with intent.setData(Uri.parse("mailto:")) can make sure that there are only Email apps in Intent chooser but it will not bring attachment(For some apps like Gmail it will, but there are also many apps that will ignore attachment).

  • Intent.ACTION_SEND can send Email with attachment. However, in Intent chooser, there will be apps that are actually not Email apps but can response to Intent.ACTION_SEND. Using intent.setType("message/rfc822") can reduce number of those apps but not all.

  • References this answer: https://stackoverflow.com/a/8550043/3952691 and nearly succeed in my goals. My code is as below:

    private static void sendFeedbackWithAttachment(Context context, String subject) {
        Intent intent = new Intent(Intent.ACTION_SENDTO);
        PackageManager packageManager = context.getPackageManager();
        List<ResolveInfo> resolveInfos = packageManager.queryIntentActivities(intent, 0);
        if (resolveInfos.isEmpty()) {
            Toast.makeText(context, context.getString(R.string.error_activity_not_found),
        } else {
            // ACTION_SEND may be replied by some apps that are not email apps. However,
            // ACTION_SENDTO doesn't allow us to choose attachment. As a result, we use
            // an ACTION_SENDTO intent with email data to filter email apps and then send
            // email with attachment by ACTION_SEND.
            List<LabeledIntent> intents = new ArrayList<>();
            Uri uri = getLatestLogUri();
            for (ResolveInfo info : resolveInfos) {
                Intent i = new Intent(Intent.ACTION_SEND);
                i.setClassName(info.activityInfo.packageName, info.activityInfo.name);
                i.putExtra(Intent.EXTRA_EMAIL, new String[] { Def.Meta.FEEDBACK_EMAIL });
                i.putExtra(Intent.EXTRA_SUBJECT, subject);
                i.putExtra(Intent.EXTRA_STREAM, uri);
                intents.add(new LabeledIntent(i, info.activityInfo.packageName,
                        info.loadLabel(context.getPackageManager()), info.icon));
            Intent chooser = Intent.createChooser(intents.remove(0),
                    intents.toArray(new LabeledIntent[intents.size()]));

    However, on some devices(For example, Xiaomi 2S with MIUI V5, I don’t know if this can be influenced by a third-party rom), the result is an empty Intent chooser. And it seems that above Android 6.0, Intent.EXTRA_INITIAL_INTENTS has some bugs(Custom intent-chooser – why on Android 6 does it show empty cells?, and another one: https://code.google.com/p/android/issues/detail?id=202693).

As a result, I don’t know how to achieve my goals. Please help me, thank you in advance.

How to&Answers:

Try the below code to Send a mail

String filename="filename.vcf"; 
File filelocation = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), filename);
Uri path = Uri.fromFile(filelocation); 
Intent emailIntent = new Intent(Intent.ACTION_SEND);
// set the type to 'email'
emailIntent .setType("vnd.android.cursor.dir/email");
String to[] = {"[email protected]"};
emailIntent .putExtra(Intent.EXTRA_EMAIL, to);
// the attachment
emailIntent .putExtra(Intent.EXTRA_STREAM, path);
// the mail subject
emailIntent .putExtra(Intent.EXTRA_SUBJECT, "Subject");
startActivity(Intent.createChooser(emailIntent , "Send email..."));


There are two ways to do this


 Intent emailIntent = new Intent(

     //Option 1
    Uri data = Uri.parse("mailto:?subject=" + "blah blah subject"
            + "&body=" + "blah blah body" + "&to=" + "[email protected]");

    startActivity(Intent.createChooser(emailIntent, ""));


enter image description here


It works perfactly except it wont filter out FTP

    //Option 2
  emailIntent = new Intent(
    final String[] toRecipients = new String[] { "[email protected]", "", };
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, toRecipients);
    emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "blah blah subject");
            Html.fromHtml("blah blah body"));

    startActivity(Intent.createChooser(emailIntent, ""));


enter image description here

Both ways have minor flaws I show you both ways it is now upto you to pick one.