Attachment is not coming in mail programmatically

I am attaching a TEXT file to Email with code :

Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto",
                                    "abc@gmail.com", null));

    emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Report");

    emailIntent.putExtra(Intent.EXTRA_TEXT, prepareBodyMail());
    File root = Environment.getExternalStorageDirectory();
    File file = new File(root, "/MyFolder/report.txt");

    Uri uri = Uri.fromFile(file);
    emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
    startActivity(Intent.createChooser(emailIntent, "Pick an Email provider"));

This code works perfectly with Gmail, Email and other apps

  • Android Studio Git .gitignore vs Project > Settings > Version Control > Ignored Files
  • Play store Beta Testing doesn't work
  • swap fragment in an activity via animation
  • Charles proxy fails on SSL Connect Method
  • Tutorial for android application with PHONEGAP
  • Failure on build with Gradle on command line with an android studio project : Xlint error
  • But this is not attaching file with INBOX application by Google

    Only Body and subject are coming without any attachment

    I have reported this problem to Google Groups at Inbox Problem

    Can anybody help what I am missing in code?

  • Should I learn Java before learning Android
  • Android Material Design: Failed to find style 'toolbarStyle' in current theme
  • IllegalArgumentException: Service not registered:
  • Avoid passing null as the view root (need to resolve layout parameters on the inflated layout's root element)
  • IllegalStateException: Can't change tag of fragment was android:switcher now android:switcher
  • Margin inside buttons
  • 4 Solutions collect form web for “Attachment is not coming in mail programmatically”

    Try this

         Uri myUri = Uri.parse("file://" + path);
         emailIntent.putExtra(Intent.EXTRA_STREAM, myUri);
    
    String fileLocation = Environment.getExternalStorageDirectory() + "/MyFolder/report.txt";    
    String to[] = {"abc@gmail.com"};
    
    Intent intentEmail = new Intent(Intent.ACTION_SEND);
    intentEmail.setType("vnd.android.cursor.dir/email");
    intentEmail.putExtra(Intent.EXTRA_EMAIL, to);
    intentEmail.putExtra(Intent.EXTRA_STREAM, fileLocation);
    intentEmail.putExtra(Intent.EXTRA_SUBJECT, "Subject");
    
    startActivity(Intent.createChooser(intentEmail , "Pick an Email provider"));
    
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("text/plain");
    intent.putExtra(Intent.EXTRA_EMAIL, new String[] {"email@example.com"});
    intent.putExtra(Intent.EXTRA_SUBJECT, "subject here");
    intent.putExtra(Intent.EXTRA_TEXT, "body text");
    File root = Environment.getExternalStorageDirectory();
    File file = new File(root, xmlFilename);
    if (!file.exists() || !file.canRead()) {
        Toast.makeText(this, "Attachment Error", Toast.LENGTH_SHORT).show();
        finish();
        return;
    }
    Uri uri = Uri.fromFile("file://" + file);
    intent.putExtra(Intent.EXTRA_STREAM, uri);
    startActivity(Intent.createChooser(intent, "Send email..."));
    
      public void sendMailWithIntent(String emailTo,
                                       String subject, String emailText, List<String> filePaths) {
            try {
                //need to "send multiple" to get more than one attachment
                final Intent emailIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
                emailIntent.setType("text/plain");
                emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
                        Util.extractEmails(emailTo));
    //            emailIntent.putExtra(android.content.Intent.EXTRA_CC,
    //                    new String[]{emailCC});
                emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
                emailIntent.putExtra(Intent.EXTRA_TEXT, emailText);
                ArrayList<Uri> uris = new ArrayList<Uri>();
                //has to be an ArrayList
                if (filePaths != null) {
                    //convert from paths to Android friendly Parcelable Uri's
                    for (String file : filePaths) {
                        File fileIn = new File(file);
                        Uri u = Uri.fromFile(fileIn);
                        uris.add(u);
                    }
                }
                emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
                Intent chooser = Intent.createChooser(emailIntent, "Send mail...");
                activity.startActivityForResult(chooser, 1);
            } catch (Exception e) {
                new ShowToast(context, e.getMessage());
            }
    
        }
    

    Call method

        List<String> list = new ArrayList<>();
        list.add(TO_ATTACH_ONE);
        list.add(TO_ATTACH_TWO);
        sendMailWithIntent(toAddresses, subject, body, list);
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.