Reputation: 53
Right now I am using ACTION_SEND but there are too many apps popup for choose so I want to use ACTION_SENDTO, but I also need to attach a few files(.zip or images) to email body for sending, I checked almost all comments of this site, almost no help, can anyone help me?
Upvotes: 1
Views: 894
Reputation: 577
This answer's a little late, but hopefully it will help those who have the same question as the original poster.
The following solution will address the OP's two requests (1) selectively display the apps from which the user can select and (2) attach multiple files to the email body.
First, we will tackle #(2). This is an expansion to the solution already posted by Ravi Rupareliya. Let's say we need to attach 2 files named "file01" and "file02". We would do so with:
ArrayList<Uri> uriList = new ArrayList<Uri>();
Uri uri = Uri.fromFile(new File("file01"));
uriList.add(uri);
Uri uri02 = Uri.fromFile(new File("file02"));
uriList.add(uri02);
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
emailIntent.setType("message/rfc822");
emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uriList);
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, *"email title"*);
emailIntent.putExtra(Intent.EXTRA_TEXT, *"email body"*);
When creating the intent, the use of ACTION_SEND_MULTIPLE (instead of simply ACTION_SEND) indicates you want multiple files attached to the email. Additionally, use the Content-Type "message/rfc822" indicates that the body contains an encapsulated message (i.e. your "file01" and "file02" attachments) that is formatted with the RFC 822 standard.
Second, we will tackle #(1). I propose that you provide an option in your settings module that allows a user to select an email client from a list of email apps. The selected email client will be the default email that gets launched whenever the user needs to send an email. This proposition requires the user to select from a list of email apps only once, not every time an email is sent.
This list of email apps can be acquired by:
Intent queryIntent = new Intent(ACTION_SENDTO);
queryIntent.setData(Uri.parse("mailto:"));
queryIntent.setType("message/rfc822");
List intentList = new ArrayList();
PackageManager packageManager = getPackageManager();
List resolveInfoList = packageManager.queryIntentActivities(queryIntent, 0);
for (int i = 0; i < resolveInfoList.size(); i++) {
ResolveInfo resolveInfo = (ResolveInfo) resolveInfoList.get(i);
String packageName = resolveInfo.activityInfo.packageName;
intentList.add(packageName);
}
Once you have acquired the list, create a module to let your user select the email app (and thereby, the packageName).
Next, to the attachment code in #(1) above, we simply add the line: emailIntent.setPackage(selectedPackageName); where selectedPackageName (String) is the email app the user has selected.
The email attachment code in its entirety will look like:
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
emailIntent.setType("message/rfc822");
emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uriList);
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, *"email title"*);
emailIntent.putExtra(Intent.EXTRA_TEXT, *"email body"*);
emailIntent.setPackage(selectedPackageName);
Upvotes: 3
Reputation: 13588
Set type of data so that you can see less apps in the popup. you need not use ACTION_SENDTO
emailIntent.setType("message/rfc822");
use this if you want to share only to gmail app.
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setData(Uri.parse("mailto:"));
emailIntent.setType("message/rfc822");
emailIntent.setPackage("com.google.android.gm"); //
emailIntent.putExtra(Intent.EXTRA_SUBJECT,subject);
emailIntent.putExtra(Intent.EXTRA_TEXT,body);
emailIntent.putExtra(Intent.EXTRA_STREAM, fileuri);
startActivity(emailIntent);
Edit: This will show all mainstream email apps:
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setData(Uri.parse("mailto:"));
emailIntent.setType("message/rfc822");
emailIntent.putExtra(Intent.EXTRA_SUBJECT,subject);
emailIntent.putExtra(Intent.EXTRA_TEXT,body);
emailIntent.putExtra(Intent.EXTRA_STREAM, fileuri);
startActivity(Intent.createChooser(emailIntent,"Send via"));
Upvotes: 2
Reputation: 2405
Try this ,this example for text ,if you want to send media file you just modify this code.
String strmail=messageToPost;
strmail=strmail.replace("<html>", "");
strmail=strmail.replace("<br/>", "");
strmail=strmail.replace("</html>", "");
strmail=strmail.replace("<b>", "");
strmail=strmail.replace("</b>", "");
// strmail="<html>"+strmail+"</html>";
String[] mailto = { "" };
final Intent sendIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:"));
sendIntent.putExtra(Intent.EXTRA_EMAIL, mailto);
sendIntent.putExtra(Intent.EXTRA_SUBJECT,
"Mail From application");
sendIntent.putExtra( Intent.EXTRA_TEXT,strmail);
startActivity(Intent.createChooser(sendIntent, "your app"));
Upvotes: 0
Reputation: 35549
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
emailIntent.setType("plain/text");
ArrayList<Uri> listUri=new ArrayList<Uri>();
Uri URI;=Uri.fromFile(new File(filename));
listUri.add(URI);
Uri URI2;=Uri.fromFile(new File(filename2));
listUri.add(URI2);
emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, listUri);
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject");
startActivity(Intent.createChooser(emailIntent,"Send"));
Upvotes: 0