Android Multiple Email Attachments Using Intent

Android Email intent with multiple attachments

I've solved it. When the images are chosen they are put into the ArrayList "userSelectedImageUriList" in onActivityResult.

Instead of parsing this list in the sendReport method, I just add this list to the intent.

The working method looks like this:

public void sendReport(Context context) {
EditText nameField = (EditText)findViewById(R.id.EditTextName);
EditText emailField = (EditText)findViewById(R.id.EditTextEmail);
EditText locationField = (EditText)findViewById(R.id.EditTextLocation);
EditText dateField = (EditText)findViewById(R.id.EditTextDate);
EditText bodyField = (EditText)findViewById(R.id.EditTextBody);

if(!nameField.getText().toString().matches("") && !emailField.getText().toString().matches("") && !locationField.getText().toString().matches("") && !dateField.getText().toString().matches("") && !bodyField.getText().toString().matches("")) {
PreferenceManager.getDefaultSharedPreferences(getBaseContext()).edit().putString("report_email", emailField.getText().toString()).apply();
PreferenceManager.getDefaultSharedPreferences(getBaseContext()).edit().putString("report_name", nameField.getText().toString()).apply();

String emailBody = "Name: "+nameField.getText()+"\n\n";
emailBody += "Location: "+locationField.getText()+"\n\n";
emailBody += "Time: "+dateField.getText()+"\n\n";
emailBody += "Description: "+bodyField.getText();

Intent emailIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);

// set the type to 'email'
emailIntent.setType("text/plain");
String to[] = {emailField.getText().toString()};
emailIntent.putExtra(Intent.EXTRA_EMAIL, to);

// the attachment - ArrayList populated in onActivityResult
emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, userSelectedImageUriList);

// the mail
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Report);
emailIntent.putExtra(Intent.EXTRA_TEXT, emailBody);

startActivity(Intent.createChooser(emailIntent , "Send with..."));
//finish();
} else {
Toast.makeText(this, getString(R.string.field_error), Toast.LENGTH_SHORT).show();
}
}

Sending data with multiple attachments with email intent

So here, we will be using two intents: selectorIntent and emailIntent. selectorIntent is what the emailIntent will use as to show available apps. code:

Intent selectorIntent = new Intent(Intent.ACTION_SENDTO);
selectorIntent.setData(Uri.parse("mailto:"));

final Intent emailIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
emailIntent.putExtra(Intent.EXTRA_EMAIL, emails);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
emailIntent.setSelector(selectorIntent);
if(emailIntent.resolveActivity(getPackageManager()) != null){
startActivity(emailIntent);
}
else {
Snackbar.make(scrollView, "Sorry, We couldn't find any email client apps!", Snackbar.LENGTH_SHORT).show();
}

Now it will choose only apps which are email client.

If there is only one email-client app in your phone than it will directly open that. And if no such application is there, than the code will show Snackbar given in the else part.

Android send email with multiple attachment using file provider

Found a solution with the following code :

 xml/provider_paths
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>

File baseDir = new File(Environment.getExternalStoragePublicDirectory (Environment.DIRECTORY_DOCUMENTS), "ABC");


if (!baseDir.exists()) {
baseDir.mkdirs();
}

Log.i("Debug", "baseDir " + baseDir.toString());
File f1 = new File(baseDir, "File1.txt");
writeToFile(f1, "This is file 1 contents 888888888");
Log.i("Debug", "f1 path" + f1.getAbsolutePath());
File f2 = new File(baseDir, "File2.txt");
writeToFile(f2, "This is file 2 contents 123456");
Log.i("Debug", "f2 path" + f2.getAbsolutePath());


String f1path = f1.toString();
String f2path = f2.toString();
string EXTRA_RECIPIENT = "janedoe@abc.com";
String message = "Test message 12345678 this is a test. ";
sendMail(f1path, f2path, message, EXTRA_RECIPIENT);


private void sendMail(String f1path, String f2path, String message, String mailTo) {
Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "Test multiple attachments");
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{mailTo});

File f1new = new File(f1path);
File f2new = new File(f2path);

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

uris.add(FileProvider.getUriForFile(MainActivity.this,
BuildConfig.APPLICATION_ID + ".provider", f1new ));
uris.add(FileProvider.getUriForFile(MainActivity.this,
BuildConfig.APPLICATION_ID + ".provider", f2new ));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.putExtra(Intent.EXTRA_TEXT, message);
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
startActivity(intent);
}

Send multiple attachments( images and PDFs) to gmail through intent

Code for file chooser:

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE,true);
startActivityForResult(Intent.createChooser(intent, "ChooseFile"), PICKFILE_RESULT_CODE);

Code for intent:

Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND_MULTIPLE);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Intent.EXTRA_EMAIL, subarray);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setType("vnd.android.cursor.dir/email");
ArrayList<Uri> uris=new ArrayList<Uri>();
if (uriMap.size()!=0) {
Iterator i= uriMap.entrySet().iterator();
while(i.hasNext()){
Map.Entry mapElement = (Map.Entry) i.next();
uris.add((Uri)mapElement.getValue());
}
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM,uris);
}
intent.setPackage("com.google.android.gm");
startActivityForResult(intent, 101);

using intent.setType() and intent.setPackage() worked for me!

Opening an email with multiple attachments, while restricting the chooser to ONLY email apps?

I finally found a solution, albeit one that only works on Ice Cream Sandwich MR1 and above. The trick is to first build your intent using ACTION_SEND_MULTIPLE:

sendEmailIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
sendEmailIntent.setType("message/rfc822");
sendEmailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { "some@email.com" });
sendEmailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
sendEmailIntent.putExtra(Intent.EXTRA_TEXT, "Body");
final ArrayList<Uri> uris = /* ... Your code to build the attachments. */
sendEmailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);

To restrict it to email apps only, add this code:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
sendEmailIntent.setType(null); // If we're using a selector, then clear the type to null. I don't know why this is needed, but it doesn't work without it.
final Intent restrictIntent = new Intent(Intent.ACTION_SENDTO);
Uri data = Uri.parse("mailto:?to=some@email.com");
restrictIntent.setData(data);
sendEmailIntent.setSelector(restrictIntent);
}

When you fire this intent with startActivity(), you'll now only see email apps in the list, and if you select Gmail, the multiple attachments will be there.

I do this with a try/catch in case startActivity resolves to no activities, in which case I remove the selector, and it seems to work well.

sending an e-mail with multiple attachments

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:"));

how to attach multiple files to email client in android

That works:

final Intent ei = new Intent(Intent.ACTION_SEND_MULTIPLE);
ei.setType("plain/text");
ei.putExtra(Intent.EXTRA_EMAIL, new String[] {"me@somewhere.nodomain"});
ei.putExtra(Intent.EXTRA_SUBJECT, "That one works");

then add files' uris:

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

ei.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
startActivityForResult(Intent.createChooser(ei, "Sending multiple attachment"), 12345);

Hope that helps.

Cant send email with multiple attachment in android programmatically

I didn't change any code. But after running it today, its working perfectly. Don't know which one wrong or right. Anyway, now its working fine for me, the same code which i posted. Thank You Alok Nair for such a help.



Related Topics



Leave a reply



Submit