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.
Related
In my application, I'm able to write to and read from a file - either in internal or external storage. Both work for reading/writing. But, I can't attach a file from either place to an email that I'm causing to be sent (via an intent) from my app.
I've requested external storage permissions both in the manifest and at runtime in the app, and I've verified that I have them (again, I'm able to read/write from my app.) In fact everything appears to be working. My email client launches with the subject and email body correctly populated, and it shows that my file is attached (I get the paperclip icon by the correct file name and everything.) But, when the email is sent, the attachment gets dropped.
I'm afraid that this might just be Google/Android refusing to really allow me to pass an attachment from one app to another.
Can anybody help?
Here's the code where I'm setting up the email attachment:
String[] TO = {"myemail#gmail.com"};
String[] CC = {""};
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setData(Uri.parse("mailto:"));
emailIntent.setType("text/plain");
emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
emailIntent.putExtra(Intent.EXTRA_CC, CC);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Something cool");
File attachment = new File(root, "TestFile2.txt");
Uri fileUri;
if (!attachment.exists() || !attachment.canRead()) {
Log.d("MyMessages", "Could not find file to attach.");
} else {
fileUri = Uri.parse("file://" + attachment.getAbsolutePath());
emailIntent.putExtra(Intent.EXTRA_STREAM, fileUri);
emailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
emailIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
emailIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
emailIntent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
Log.d("MyMessages", "I think I attached a file...");
}
emailIntent.putExtra(Intent.EXTRA_TEXT, "Here you go...\n");
try {
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
finish();
Log.d("MyMessages", "Email sent.");
} catch (android.content.ActivityNotFoundException ex) {
Log.d("MyMessages", "Email not sent. Client installed?");
}
Good day!
So sorry, but at this moment Wear 2.0 doesn´t support neither Intent mechanism for initiating other wear apps, nor email sending via 3-d party email client.
I have spent 3 week in searching the solution and didn't find it.
So I'm trying to launch a prepopulated email client with data. The content gets populated fine, however my problem is that when launching the intent, I wanted it to only show email clients to select from.
Instead, it shows Gmail, Adding to EverNote, Android Beam, Bluetooth, and some others.
I don't know if its an issue with lollipop that broke this functionality or not, as one of my managers sent me code that worked fine for him a few years ago.
My code is:
private void openEmailClient(){
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("message/rfc822");
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{getResources().getString(R.string.contact_feedback_email_address)});
intent.putExtra(Intent.EXTRA_SUBJECT, getResources().getString(R.string.contact_feedback_email_subject_android));
try{
startActivity(Intent.createChooser(intent,intentEmailString));
} catch(android.content.ActivityNotFoundException ex){
Log.e(EMAIL_FAIL_TAG, EMAIL_FAIL);
ex.printStackTrace();
}
}
when you will change your intent.setType like below you will get
intent.setType("text/plain");
Use
android.content.Intent.ACTION_SENDTO
(new Intent(Intent.ACTION_SENDTO);) to get only the list of e-mail clients, with no facebook or other apps. Just the email clients.
I wouldn't suggest you get directly to the email app. Let the user choose his favorite email app. Don't constrain him.
If you use ACTION_SENDTO, putExtra does not work to add subject and text to the intent. Use Uri to add the subject and body text.
We can use message/rfc822 instead of "text/plain" as the MIME type. However, that is not indicating "only offer email clients" -- it indicates "offer anything that supports message/rfc822 data". That could readily include some application that are not email clients.
message/rfc822 supports MIME Types of .mhtml, .mht, .mime
EDIT
Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:someone#example.com"));
intent.putExtra("subject", "my subject");
intent.putExtra("body", "my message");
startActivity(intent);
its working ...
So I solved it. Not ideally but it works better than anything else I have tried.
I followed the google docs on doing it, which says to do this:
public void composeEmail(String[] addresses, String subject) {
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:")); // only email apps should handle this
intent.putExtra(Intent.EXTRA_EMAIL, addresses);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
and it now works.
This just finds a default app for handling mail. I'm not sure how it decides, but in my case it opened GMail. On a device without GMail installed, such as the Galaxy S5, it opened their mail client and prompted the user to set up email.
Doesn't give choice of app but it works
Try like this it working fine for me...
Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:abc#gmail.com"));
intent.putExtra(Intent.EXTRA_SUBJECT, "Test App");
intent.putExtra(Intent.EXTRA_TEXT, "Email Body");
startActivity(intent);
Note: it only work if you have email address.
For more information please refer this link Android - Is there a foolproof way to only show possible EMAIL clients?
I succeded in sending an intent to skype and calling whoever i want but now i want to be able to send IM and send files,all done in the background.Is that even possible ? I've looked over their developer page for android,it's kind of poor and doesn't say anything about this.
So,is there any way to do that ?
Thanks in advance and have a nice day !
I have almost succeeded in sending files over Skype using Intents. I am actually using one and the same intent for sending over email or skype, and I let the user choose the app they want to use to send the file. My code is:
File readF = new File(fullFileName);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/*");
intent.putExtra(Intent.EXTRA_SUBJECT, getResources().getString(R.string.send_message_subject) + " " + myList.getName());
intent.putExtra(Intent.EXTRA_TEXT, getResources().getString(R.string.send_message_body));
Uri uri = FileProvider.getUriForFile(this, "my.package.fileprovider", readF);
intent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(intent, "Sending..."));
The File provider I use for sending the file is just like the one explained here: https://developer.android.com/reference/android/content/ContentProvider.html
If you like to send the file explicitly through Skype, and not let the user choose the app maybe you can use something like this:
sharingIntent.setClassName("com.twitter.android","com.twitter.android.PostActivity");
(explained here)
Why I ALMOST succeeded is that the filename is changed when sending over Skype. I haven't figured out how to fix this yet.
Thanks in advance for your help.
I am developing an Android App that, as part of its core functionality, needs to send emails between users (peer-to-peer emails, not spam). These emails need to contain a link that will open the Android App upon a user-click
The problem I am having is: when I send these emails to gmail acounts, links appear as normal text rather than as links.
Here is my code
private void sendEmail(String recepientName, String recipientEmail) {
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
String aEmailList[] = { recipientEmail };
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, aEmailList);
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "My Title");
emailIntent.setType("text/html");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml(
"<!DOCTYPE html><html><body>" +
"<br>Dear " + recepientName + ",<br>" +
"Please <font>click here</font></body></html>"));
startActivityForResult(emailIntent, EMAIL_REQUEST);
}
What should I do to make these link work in received emailsin gmail ?
Again Thanks
What I discovered was that somehow (probably with great talent) I got into Google's black list.
this means that Google stripped the link out of my code and the users saw it as regular text
HERE IS THE REASONING:
My link looked like this: myAppName://parameter1/parameter2/Parameter3
The prefix ("myAppName://") allows Android to identify my App and launch it as you click on the link.
HOWEVER:
When this link was sent to a gmail account, Google's servers were able to identify that this was an invalid link (pretty cool that they check that ha!!) and they stripped the link out.
The solution was to use a real URL for App identification and Launch:
Something like: http://myhost.com/parameter1/parameter2/Parameter3
Hope that helps
android:scheme="http" and the appropriate android:host
My application uses the intent method to send out emails to users, as a convenient way of exporting Excel spreadsheet data (created by the JExcell API).
The file is contained on the SD card in a folder called records.
The file I am attempting to send is call measurments.xls.
I have tested in code for the existence of the file prior to sending. The email composer shows an attachment, but when I send and then receive the email the attachment is not there.
However, if I substitute the excel file for a png image, the attachment is received. So what gives??
Below is the code I use to send out the email, it is simply a paramiterised static method in a class by its self.
public static void sendEmailWithAttachment(Context ctx, String to,String subject, String message, String fileAndLocation)
{
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("text/plain");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] {to});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, message);
File file = new File(fileAndLocation);
// File file = getFileStreamPath();
if (file.exists())
{
Log.v("Farmgraze", "Email file_exists!" );
}
else
{
Log.v("Farmgraze", "Email file does not exist!" );
}
Log.v("FarmGraze", "SEND EMAIL FileUri=" + Uri.parse("file:/"+ fileAndLocation));
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:/"+ fileAndLocation));
ctx.startActivity(Intent.createChooser(emailIntent, "Send mail..."));
}//end method
So what do I need to do to receive the xls file? Change the mime types in the second line of the method's code? If so what do. Any helpful advice would be greatly appreciated.
Thanks for reading.
A.
Ok folks just to add closure to this question I found the solution.
The problem was that the file path String sent to the URI, needs to have three forward slashes.
As in:
file:///sdcard/somefolder/some_file.xls.
Also for excel documents one needs to set the type as follows:
emailIntent.setType("application/excel");
So the problem was a two pronged one. I was aware of the three slash solution via this thread, but as it did not work thought the problem lie else where.
Also I became aware of the correct mime types via this webpage which lists all of the supported mime types, and may be very useful for other readers.
So thanks for reading and taking an interest in my little problem which is now solved.
I think the issue might be with your mime-type. Try this:
MimeTypeMap mime = MimeTypeMap.getSingleton();
String mimeTypeForXLSFile = mime.getMimeTypeFromExtension(".xls");
emailIntent.setType(mimeTypeForXLSFile);