sending an e-mail with multiple attachments
Asked Answered
M

3

7

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[] {"[email protected]", "[email protected]"});
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...

Merger answered 16/3, 2011 at 18:15 Comment(0)
S
13

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:"));
Sextan answered 5/8, 2011 at 6:52 Comment(3)
+2 android.content.Intent.ACTION_SEND_MULTIPLE this works fine...thanxAlvinaalvine
If you don't pass a new String[] {} to Intent.EXTRA_EMAIL it'll just leave the recipient field empty...Triad
here where to add recipient addressMate
G
0

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.");'

Gooseberry answered 10/1, 2014 at 23:20 Comment(1)
if emailIntent.setType("text/html"), your solution does work for HTML body.Stria
R
0

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.

Rondi answered 22/8, 2018 at 8:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.