I have a weird situation here.
I am trying to send emails with multiple attachments using the following piece of code.
Intent emailIntent = new Intent( android.content.Intent.ACTION_SEND_MULTIPLE );
// emailIntent.setType( "plain/text" );
emailIntent.setType( "application/octet-stream" );
...
....
emailIntent.putParcelableArrayListExtra( Intent.EXTRA_STREAM, uris );
This works fine and the implicit intent mechanism shows up a lot of options like Gmail, Skype, Messaging etc.
The problem is that the default Mail client does not show up on HTC Thunderbolt ( but works on other devices including HTC Incredible S ).
If I try to send a single attachment using Intent.ACTION_SEND, the default mail client shows up. I have tried setting content type to text/plain, appliation/octet-stream, message/rfc282 etc but none works.
What am I missing here?
I had the same issue, I Fixed it with using http Mime Library for multipart form entity.
here is the link to the file.
http://hc.apache.org/httpcomponents-client-4.3.x/httpmime/apidocs/org/apache/http/entity/mime/HttpMultipart.html
Sounds like a bug in the Thunderbolt's version of Sense. Custom UIs for the win, am I right?
Anyway, I would look up what app actually does handle emails on the thunderbolt and put an if-statement to detect if the device is a thunderbolt. If it is, set the Intent's target class to whatever that is. If it's not, do what you're already doing.
This works great for me, be sure to specify the the message type, that is how the android os knows which broadcast to use.
String email = "test#email.com";
Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
intent.setType("message/rfc822");
intent.putExtra(Intent.EXTRA_EMAIL, new String[] {email}); // could have multiple address
intent.putExtra(Intent.EXTRA_SUBJECT, "Enter your subject here");
intent.putExtra(Intent.EXTRA_TEXT, "message text as needed");
ArrayList<Uri> arrayUri = new ArrayList<Uri>();
arrayUri.add(Uri.parse("file://" + paths[0]));
arrayUri.add(Uri.parse("file://" + paths[1]));
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, arrayUri);
startActivity(Intent.createChooser(intent, "Any title to show on chooser"));
Try this. I think it will work.
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:"));
Related
I have a FeedbackActivity.java activity which takes feedback from user with multiple attachments (upto 3 images as attachments).
I am using following code:
Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
intent.setType("message/rfc822");
intent.putExtra(Intent.EXTRA_EMAIL, emails); //emails is an Array of 'String' type
intent.putExtra(Intent.EXTRA_SUBJECT, subject); //subject is a String
intent.putExtra(Intent.EXTRA_TEXT, text) //text is a String
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris); //uris is an ArrayList of 'Uri' type
//uris stores all Uri of images selected
if(intent.resolveActivity(getPackageManager()) != null){
startActivity(intent);
}
else {
Toast.makeText(this, "Not Good", Toast.LENGTH_SHORT).show();
}
Now this code works fine but the problem is that it shows all sorts of apps which support "message/rfc822" MIME type.
Image is shown below :
I only need to show the email client apps, I tried Uri.parse("mailto:"), but didn't workout and code always moves to else statement and shows the toast "not good".
I read the google documentation but it only shows simple cases.
I tried searching on the web. Many developers are using intent.setType("*/*") or intent.setType("text/plain"). But they all too show apps other than email clients.
Please guide me.
And I wanted to ask in general,
Google documentations show simple examples which is good in a way, but how to learn really in depth on these kind of topics?
Thank you.
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.
Don't use Uri.parse, use Uri.fromParts
Do it like this:
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto","example#mail.com", null));
I am trying to attach multiple file using intent.
below my code.
Intent mSendIntent = new Intent(android.content.Intent.ACTION_SEND);
mSendIntent.setType("plain/text");
mSendIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
new String[] { "" });
mSendIntent.putExtra(android.content.Intent.EXTRA_CC, "");
mSendIntent.putExtra(android.content.Intent.EXTRA_BCC, "");
mSendIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
"InfoMe Profile Request");
ArrayList<String> yList = new ArrayList<String>();
yList.add(path+filename);
yList.add(path+"save.ime");
ArrayList<Uri> y = new ArrayList<Uri>();
for(String a:yList){
y.add(Uri.fromFile(new File(a)));
}
mSendIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM,
y);
mSendIntent.putExtra(android.content.Intent.EXTRA_TEXT,
Html.fromHtml(bodypart));
context.startActivity(Intent.createChooser(mSendIntent,
"Send mail..."));
but when i sent it than no attachment found. please help me thanks in advance.
This is a nice tutorial on how to send multiple images via attachment: http://android-er.blogspot.hk/2012/10/start-activity-to-send-multi-images.html
FYI, ACTION_SEND is for a single attachment. For multiple files, you need ACTION_SEND_MULTIPLE
The gist of it is, when you click on the button to invoke the Intent to send an email, you need something like this:
intent.setAction(Intent.ACTION_SEND_MULTIPLE);
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, arrayUri);
intent.setType("image/*");
startActivity(Intent.createChooser(intent, "Choice App to send email:"));
Here the arrayUri is declared like this: ArrayList<Uri> arrayUri = new ArrayList<Uri>();
Follow the tutorial for a complete functioning example. They also have the entire project available for download.
Your code is clean until that
Intent mailIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
I can get both Mail and Gmail to attach multiple csv files to an email.
When sent via Mail all the attachments are delivered.
When sent by Gmail none of the attachments are delivered.
I have read the documentation Send Binary Content. I have searched but only found a solution for Gmail that does not work with Mail. Mail seems happy with just about any approach. Gmail just doesn't want to play.
Has anyone found a solution for sending multiple attachments that works with both Mail and Gmail?
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
String subject = context.getString(R.string.export_data_email_header);
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
emailIntent.setType("text/csv");
ArrayList<Uri> uris = new ArrayList<Uri>();
if (diariesSelected) uris.add(Uri.fromFile(context.getFileStreamPath("diaries.csv")));
...
emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
context.startActivity(emailIntent);
And the code used to create the file
FileOutputStream fos = context.openFileOutput(path, Context.MODE_WORLD_READABLE);
OutputStreamWriter writer = new OutputStreamWriter(fos);
writer.append(builder.toString());
writer.close();
fos.close();
The following code is a snippet from one of my apps. As far as I remember, it works with GMail and Mail (Can't verify it at the moment.). It looks basically like your solution, but with a few little differences. Maybe one of them is what you are looking for. :)
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
emailIntent.setType("text/plain");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] { "address#mail.com" });
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "The subject");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "The actual message");
ArrayList<Uri> attachmentUris = new ArrayList<Uri>();
for (File attachment : attachments) {
attachmentUris.add(Uri.fromFile(attachment));
}
emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, attachmentUris);
startActivity(emailIntent);
Here you can get detail information https://stackoverflow.com/a/18225100/942224
by using below code i am attaching image file in gmail or Mail.... hope it will help you
Intent ei = new Intent(Intent.ACTION_SEND_MULTIPLE);
ei.setType("plain/text");
ei.putExtra(Intent.EXTRA_EMAIL, new String[] {"email id"});
ei.putExtra(Intent.EXTRA_SUBJECT, "That one works");
ArrayList<String> fileList = new ArrayList<String>();
fileList.add(Environment.getExternalStorageDirectory()+"/foldername/certi/qualifications.jpg");
fileList.add(Environment.getExternalStorageDirectory()+"/foldername/certi/certificate.jpg");
fileList.add(Environment.getExternalStorageDirectory()+"/foldername/Aa.pdf");
ArrayList<Uri> uris = new ArrayList<Uri>();
//convert from paths to Android friendly Parcelable Uri's
for (int i=0;i<fileList.size();i++)
{
File fileIn = new File(fileList.get(i));
Uri u = Uri.fromFile(fileIn);
uris.add(u);
}
ei.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
startActivityForResult(Intent.createChooser(ei, "Sending multiple attachment"), 12345);
I'm trying to send multiple attachments in an Intent to the Email app (not the Gmail app). I'm using:
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
emailIntent.setType("plain/text");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,new String[] { "sample#email.com" });
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"This is an email");
emailIntent.putExtra(Intent.EXTRA_TEXT, "This is the body");
File f1 = null;
File f2 = null;
try {
f1 = new File("/sdcard/test");
f2 = new File("/sdcard/test.1");
FileWriter fw1 = new FileWriter(f1);
FileWriter fw2 = new FileWriter(f2);
fw1.write("this is some text");
fw2.write("this is more text");
fw1.close();
fw2.close();
} catch (IOException e) {
e.printStackTrace();
}
ArrayList<Uri> uris = new ArrayList<Uri>();
uris.add(Uri.fromFile(f1));
uris.add(Uri.fromFile(f2));
emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM,uris);
startActivity(emailIntent);
When Gmail is used to handle the Intent, it comes up with both attachments showing, and everything works just fine. When the Email app is used instead, no attachments are added. When using a single Uri in EXTRA_STREAM, the single attachment works, but using an ArrayList does not. I've pieced together this code from other questions asked on here, but none of them resolve this issue. Can anyone help?
use
emailIntent.setType(" */ * ");
with no spaces
see here ACTION_SEND_MULTIPLE
I realize that this is quite late, but your intent type is backwards. It should be
emailIntent.setType("text/plain");
not
emailIntent.setType("plain/text");
I'm surprised neither of the other answers pointed that out...
Instead of
emailIntent.setType("plain/text");
use
emailIntent.setType("application/octet-stream");
I don't know why, but it's working for me.
I've been working on Android program to send email with an attachment (image file, audio file, etc) using Intent with ACTION_SEND. The program is working when email has a single attachment. I used Intent.putExtra(android.content.Intent.EXTRA_STREAM, uri) to attach the designated image file to the mail and it is working fine, the mail can be delivered through the Gmail. However, when I tried to have multiple images attached to the same mail by calling Intent.putExtra(android.content.Intent.EXTRA_STREAM, uri) multiple times, it failed to work. None of the attachment show up in the email.
I searched the SDK documentation and Android programming user group about email attachment but cannot find any related info. However, I've discovered that there's another intent constant ACTION_SEND_MULTIPLE (available since API level 4) which might meet my requirement. Based on SDK documentation, it simply states that it deliver multiple data to someone else, it works like ACTION_SEND, except the data is multiple. But I still could not figure out the correct usage for this command. I tried to declare intent with ACTION_SEND_MULTIPLE, then call putExtra(EXTRA_STREAM, uri) multiple times to attach multiple images, but I got the same erroneous result just like before, none of the attachment show up in the email.
Has anyone tried with ACTION_SEND_MULTIPLE and got it working with multiple email attachment?
Here is the code you need to create an emailIntent that contains multiple attachments.
public static void email(Context context, String emailTo, String emailCC,
String subject, String emailText, List<String> filePaths)
{
//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,
new String[]{emailTo});
emailIntent.putExtra(android.content.Intent.EXTRA_CC,
new String[]{emailCC});
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(Intent.EXTRA_TEXT, emailText);
//has to be an ArrayList
ArrayList<Uri> uris = new ArrayList<Uri>();
//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);
context.startActivity(Intent.createChooser(emailIntent, "Send mail..."));
}
ACTION_SEND_MULTIPLE should be the action
and then emailIntent.setType("text/plain");
followed by:
ArrayList<Uri> uris = new ArrayList<Uri>();
String[] filePaths = new String[] {"sdcard/sample.png", "sdcard/sample.png"};
for (String file : filePaths)
{
File fileIn = new File(file);
Uri u = Uri.fromFile(fileIn);
uris.add(u);
}
emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
startActivity(emailIntent);
This works for me.
Although this is an old thread, but as it is shown on top on google searches i want to add a small hint to make it complete, hence I stumpled upon it.
It is necessary to make the attached files readable for the mail activity, otherwise they will not be attached. So you have to call somewhere
fileIn.setReadable(true, false)
Here I found great example http://www.blackmoonit.com/2010/02/filebrowser-send-receive-intents/
you must use
final Intent aIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
aIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM,theUris);
aIntent.setType(theOverallMIMEtype);
For multiple attachments use PutParcelableArrayListExtra(Intent.ExtraStream, uris)where uris variable is a List<IParcelable>().
Here's an example:
var email = new Intent(Intent.ActionSendMultiple);
email.SetType("text/plain");
email.PutExtra(Intent.ExtraEmail, new string[]{emailTo});
email.PutExtra(Intent.ExtraCc, new string[]{emailCC});
var uris = new List<IParcelable>();
filePaths.ForEach(file=> {
var fileIn = new File(file);
var uri = Android.Net.Uri.FromFile(fileIn);
uris.Add(uri);
});
email.PutParcelableArrayListExtra(Intent.ExtraStream, uris);
context.StartActivity(Intent.CreateChooser(email, "Send mail..."));
Hope this helps ;)