sending an e-mail with multiple attachments

2019-04-07 23:19发布

问题:

i am trying to send an e-mail with multiple attachments.

Intent emailIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] {"email1@email.com", "email2@email.com"});
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "The Subject");
emailIntent.putExtra(Intent.EXTRA_TEXT, "The Text");
emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
emailIntent.setType("text/plain");
startActivity( Intent.createChooser(emailIntent, "Send Email Using: ") );

This works great when I send the email using gmail, but it doesn't attach the attachments if I send the e-mail using the e-mail client on a Nexus One. It has all the text, the subject, etc... but just no attachments. The email account I have is an exchange account if that matters...

回答1:

Try This its work fine.

final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
emailIntent.setType("plain/text");

ArrayList<Uri> uris = new ArrayList<Uri>();

String[] filePaths = new String[] {image1 Path,image2 path};
for (String file : filePaths) {
    File fileIn = new File(file);
    Uri u = Uri.fromFile(fileIn);
    uris.add(u);
}

if ( !(app_preferences.getString("email", "") == null || app_preferences.getString("email", "").equals(""))) {
    emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] {app_preferences.getString("email", "")});    
}

emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject name");
emailIntent.putExtra(Intent.EXTRA_TEXT, "Please find the attachment.");
emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);

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


回答2:

Tried all this a million times - got it to work but had a nasty warning. Found out it's an Android bug. There's a fix & more info here:

https://code.google.com/p/android/issues/detail?id=38303

Error: ClassCastException warning in log when opening e-mail app with a body and multiple file attachments.

Update: found workaround. Instead of

sendIntent.putExtra(Intent.EXTRA_TEXT, "See attached CSV files.");

Put the text as an ArrayList

ArrayList<String> extra_text = new ArrayList<String>();
extra_text.add("See attached CSV files.");
sendIntent.putStringArrayListExtra(Intent.EXTRA_TEXT, extra_text); 

Voila! No more exception, and the EXTRA_TEXT ends up as the body of the email.

EDIT: I think simply commenting out this line gets rid of the error - but then you don't get to enter any info for a body. In my case that is fine though since I'm only emailing log files. Remove this line to get rid of the warning: 'sendIntent.putExtra(Intent.EXTRA_TEXT, "See attached CSV files.");'



回答3:

If you want to send some files should pay attention! 1. Use with ACTION_SEND_MULTIPLE instead of ACTION_SEND. 2. Use with setType("text/plain") instead of setType("application/image") 3. Use with putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris) instead of putExtra(Intent.EXTRA_STREAM, imageUris)

                Intent emailIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
                emailIntent.setType("text/plain");
                emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{""});
                emailIntent.putExtra(Intent.EXTRA_SUBJECT,"SUBJECT");
                emailIntent.putExtra(Intent.EXTRA_TEXT, "BODY");

                ArrayList<Uri> imageUris = new ArrayList<>();

                imageUris.add(Uri.parse("file://" + invoicePath));
                if (signaturePath != null) {
                    imageUris.add(Uri.parse("file://" + signaturePath));
                }

                emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);
                startActivity(Intent.createChooser(emailIntent, "Send mail..."));

This works for me.