I setup an email intent in my app. I also attach a binary file with it. When the email editor opens up it shows how many bytes. But when I receive the email it is Zero Bytes!
I have done this on a separate project and it worked before so I just copied that code to the new project.
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("application/octet-stream");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] { "" });
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "message here");
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "subject");
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///data/data/com.example.app/files/filename.extension"));
I have tested it on Gingerbread and Jelly bean. I used the default email editor of the devices(Galaxy Y and HTC One).
Again the received attachment is Zero Bytes even if the email editor shows some bytes(i.e. 306 bytes)
-------UPDATE---------
So I created another project and tested the code below. It sends an email with attachment. But when I use the same code on my project the email received is zero byte. You can see I even tested if the file object is null and it is not null when ran.
File file = new File(File.getFilePath(context, "myfile.code"));
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("message/rfc822");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] { "" });
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Blah...blah...");
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "File sent");
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file)); //I also used file:///data/data/com.example.emailfile/files/myfile.code and didn't work
if(file == null){
Log.d("----FILE----", "NULL");
}else{
Log.d("----FILE----", "Not Null");
}
This is driving me crazy...any settings or setup I did on my project that prevents attachment?
Btw, the activity where this is running only has radio buttons and a button to invoke the email intent. I'm sending the email either using gmail or outlook.
Finally figured it out.
First of course make sure the file is MODE_WORLD_READABLE.
Second Gmail seems to don't have access to internal storage or files within apps. Also, discussed here http://code.google.com/p/android/issues/detail?id=18872
It managed to send and the attachment using outlook.
String body="message";
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Check out this book I am reading");
emailIntent.setType("plain/text");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, body);
startActivity(Intent.createChooser(emailIntent, "Send email..."));
No matter what I do (removing all gmail accounts and signin a hotmail account with mail app), this code launches Gmail by default and do not show or let me choose my universal mail app.
Consequently, there is no way to let user send email via hotmail or other mail provider.
update:
Actually this is the best piece of code I ever come across, it presents you directly with an app chooser where only mail client are present. The answer below will give you a huge list of apps to choose from that are irrelevant.
String mailTo="";
Intent email_intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto",mailTo, null));
email_intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject text here");
email_intent.putExtra(android.content.Intent.EXTRA_TEXT,"Body text here");
startActivity(Intent.createChooser(email_intent, "Send email..."));
Try using the correct MIME type (text/plain) instead of an invalid MIME type (plain/text).
I'm trying to attach a text file to an email and I'm getting a weird error that I hope someone can help me with. It works fine when the user selects the gmail app from the chooser, but if they select the built in mail application, they see a toast that says "Unable to attach file".
The code looks like this:
public static void sendMail(Context context, String emailBody, String emailSubject, String emailAddress, String attachmentFilename){
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("plain/text");
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { emailAddress});
emailIntent.putExtra(Intent.EXTRA_SUBJECT, emailSubject);
emailIntent.putExtra(Intent.EXTRA_TEXT, emailBody);
if(attachmentFilename != null) {
//Add the attachment by specifying a reference to our custom ContentProvider and the specific file of interest
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("content://" + Settings.VYPR_LOG_PROVIDER_AUTHORITY + "/" + attachmentFilename));
}
context.startActivity(emailIntent);
}
Anyone have any thoughts on what might be going on here? Most of what I have seen on here has to do with the attachment being on the SD card. I actually didn't write this code myself, but it seems like that must not be the issue here since it does work if the user selects the gmail app rather than the built in one.
Thanks in advance!
I experienced the same problem long time ago. Had to make the use of the Gmail app mandatory to send attachments. I could not figure out why the built-in email app did not work.
If you are trying to receive the attachment to a specific email address, you may also consider deploying a web service to upload the attachment.
Hope it helps.
I am trying to send a pdf as an attachment from Android. Here is the code:
String[] mailto = {"me#gmail.com"};
Uri uri = Uri.parse("android.resource://com.mywebsite.sendemail/raw/mypdf");
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_EMAIL, mailto);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "My Subject");
emailIntent.putExtra(Intent.EXTRA_TEXT, "My Body");
emailIntent.setType("application/pdf");
emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(emailIntent, "Send email using:"));
Now this works but the problem is that the attachment is called mypdf instead of mypdf.pdf. I cannot figure out how to send it with it's extension... That's what I need help with. Thanks.
I am unconvinced what you want will be possible, since you are pulling the PDF from a resource. If you copy the PDF to a local file (with the correct extension) and send that, you should get the extension in the resulting message. But straight out of the resource...I suspect there's no way to add the extension.
When ever I attempt to use the .putExtra methodology it always crashes my application with a "Force Close" message. If I use something like:
String mtUri = "mailto:someone#gmail.com?subject=Some Subject&body=Some text&";
Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse(mtUri));
startActivity(intent);
It seems to work fine. I do still have the problem of attaching a file and could use some help figurint out the "attachment=file:///..." syntax.
Thanks,
I am trying to launch an Intent to send an email. All of that works, but when I try to actually send the email a couple 'weird' things happen.
here is code
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("image/jpeg");
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Photo");
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://sdcard/dcim/Camera/filename.jpg"));
sendIntent.putExtra(Intent.EXTRA_TEXT, "Enjoy the photo");
startActivity(Intent.createChooser(sendIntent, "Email:"));
So if I launch using the Gmail menu context It shows the attachment, lets me type who the email is to, and edit the body & subject. No big deal. I hit send, and it sends. The only thing is the attachment does NOT get sent.
So. I figured, why not try it w/ the Email menu context (for my backup email account on my phone). It shows the attachment, but no text at all in the body or subject. When I send it, the attachment sends correctly. That would lead me to believe something is quite wrong. Do I need a new permission in the Manifest launch an intent to send email w/ attachment? What am I doing wrong?
Also getting the same problem
Code:
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("image/jpeg");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]
{"me#gmail.com"});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
"Test Subject");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,
"go on read the emails");
Log.v(getClass().getSimpleName(), "sPhotoUri=" + Uri.parse("file:/"+ sPhotoFileName));
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:/"+ sPhotoFileName));
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
From adb logcat:
V/DumbDumpersMain( 3972): sPhotoUri=file://sdcard/DumbDumpers/DumbDumper.jpg
I/ActivityManager( 56): Starting activity: Intent { action=android.intent.action.CHOOSER comp={android/com.android.internal.app.ChooserActivity} (has extras) }
I/ActivityManager( 56): Starting activity: Intent { action=android.intent.action.SEND type=jpeg/image flags=0x3000000 comp={com.google.android.gm/com.google.android.gm.ComposeActivityGmail} (has extras) }
I/ActivityManager( 56): Starting activity: Intent { action=android.intent.action.SEND type=jpeg/image flags=0x2800000 comp={com.google.android.gm/com.google.android.gm.ComposeActivity} (has extras) }
D/gmail-ls( 120): MailProvider.query: content://gmail-ls/labels/me#gmail.com(null, null)
D/Gmail ( 2507): URI FOUND:file://sdcard/DumbDumpers/DumbDumper.jpg
Looks like the email provider is attaching a 0 length file. When I check the filesystem the file is there and correct. The code which creates the image file is well finished prior to the attempt to email it.
Anyone fixed this without magic reboots (I've already tried that)?
Update
Path for me should have been
file:///sdcard/DumbDumpers/DumbDumper.jpg
you need the extra / as this points to the root directory, i.e.:
file:// + /sdcard/DumbDumpers/DumbDumper.jpg
combined as
file:///sdcard/DumbDumpers/DumbDumper.jpg
In the above snippet you need:
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+ sPhotoFileName));
Just a little remark from my side. I've been having the same issues with GMail, but somehow it seems to work when I store the file in question on the SD card first and retrieve it from there, rather than from the assets. So my code is the following:
Intent i = new Intent(Intent.ACTION_SEND);
i.putExtra(Intent.EXTRA_SUBJECT, "Title");
i.putExtra(Intent.EXTRA_TEXT, "Content");
i.putExtra(Intent.EXTRA_STREAM, uri);
i.setType("text/plain");
startActivity(Intent.createChooser(i, "Send mail"));
and here,
uri = Uri.fromFile(new File(context.getFilesDir(), FILENAME));
does not work, whereas
uri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), FILENAME));
does.
Regards,
Michael
instead of "Uri.parse" use "Uri.fromFile(new File(Environment.getExternalStorageDirectory(),"file name"))"
Environment.getExternalStorageDirectory() - path to SDcard or any other external storage
It appears that this is actually correct, not sure what was happening, but after a reboot it started working :/
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL , new String[]{"example#mail.com"});
i.putExtra(Intent.EXTRA_SUBJECT, "Data from app");
i.putExtra(Intent.EXTRA_TEXT , "experience number x");
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "filename.txt"));
i.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(i, "Send email..."));
I got the same problem and looked everywhere for a solution. Finally I solved it by finding an open source app that worked out of the box and looked at how they did it. The code is rather long so I won't quote it here but post a link. Look at the sendEmail function in line 449
http://rehearsalassist.svn.sourceforge.net/viewvc/rehearsalassist/android/trunk/src/urbanstew/RehearsalAssistant/SessionPlayback.java?revision=94&view=markup
I refactored my code to be similar, and now it works. I hope this will help others in the same situation.
From RFC 1738 section 3.10:
A file URL takes the form:
file://<host>/<path>
where host is the fully qualified domain name of the system on
which the path is accessible, and path is a hierarchical
directory path of the form directory/directory/.../name.
So it's file:///path/from/root just like http://host/path/from/root because there's an implicit 'localhost' between the second and third slash. But as mentioned above, use Uri.FromFile() to build it.
I had the same symptoms. In my case it was because I was initially saving the attachment with the permissions MODE_PRIVATE. As soon as I changed it to MODE_WORLD_READABLE it seems GMail was then able to access the file and send the attachment properly.
See more
It's work perfectly for me:
On this solution the Nicolas create one copy inside Cache folder and here gmail intent has access!
http://stephendnicholas.com/archives/974
public void sendMail(String path) {
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
new String[] {AppConstant.server_mail});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
"IBPS ERROR Mail");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,
"This is an autogenerated mail from IBPS app");
emailIntent.setType("image/png");
Uri myUri = Uri.parse("file://" + path);
emailIntent.putExtra(Intent.EXTRA_STREAM, myUri);
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
}
Also try adding Intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); This helped with my issue.
I have got solution on this after 4 days, Please note following points while giving path to File class in Android(Java):
1) Use path for internal storage String path="/storage/sdcard0/myfile.txt";
2) path="/storage/sdcard1/myfile.txt";
3) mention permissions in Manifest file.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
4) First check file length for confirmation.
5) Check paths in ES File Explorer regarding sdcard0 & sdcard1 is this same or else......
e.g.
File file=new File(path);
long=file.length();//in Bytes
Send an email with an attachment: (By docs)
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType(HTTP.PLAIN_TEXT_TYPE);
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] {"jon#example.com"});
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Email subject");
emailIntent.putExtra(Intent.EXTRA_TEXT, "Email message text");
emailIntent.putExtra(Intent.EXTRA_STREAM,
Uri.parse("content://path/to/email/attachment"));
// You can also attach multiple items by passing an ArrayList of Uris