I am developing an Android application in which I am sending multiple attachments to email client application.
To send multiple attachments I am using ACTION_SEND_MULTIPLE intent.
Code snippet:
Intent emailIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
emailIntent.setData(Uri.parse("mailto:"));
emailIntent.setType("text/plain");
emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Test"); // email subject
ArrayList<CharSequence> msg = new ArrayList<CharSequence>();
msg.add(mail_body); // email body
emailIntent.putCharSequenceArrayListExtra(Intent.EXTRA_TEXT, msg);
ArrayList<Uri> uris = new ArrayList<Uri>(); //attachments
uris.add(Uri.fromFile(logFile));
uris.add(Uri.fromFile(oldLogFile));
emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
this.startActivityForResult(Intent.createChooser(emailIntent, "Send mail..."), ErrorCodes.MAIL_ACTIVITY);
Output of the above code:
In the Gmail application, the attachments are present but email body is missing.
How can I fix this issue? I would appreciate any suggestions and thought on this topic.
First, ACTION_SEND_MULTIPLE does not use a Uri, as you have with your setData() call.
Second, ACTION_SEND_MULTIPLE uses either EXTRA_TEXT or EXTRA_STREAM, not both. See the documentation. This is the most likely source of your difficulty — you are trying to do something that is not documented to work.
Third, Uri.fromFile() will not work on Android 7.0+, if your app has a targetSdkVersion of 24 or higher. You will need to use FileProvider or some similar solution.
Related
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 use this solution but Eclipse still cant resolve "Attachment" and "DataService".
I've imported mail.jar and activation.jar, what could I be doing wrong? I've tried countless other sending email solutions on SO/Google but I couldn't get any of them to work with my attachment and send HTML emails.
Any help would be appreciated.
If you need to construct email to be sent by user manually (open his mail program with new email and attachment) you can use this code:
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setData(Uri.parse("mailto:" ));
emailIntent.setType("message/rfc822");
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "SUBJECT");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "MESSAGE");
File toAttach = new File("/path/to/your/file");
Uri uri = Uri.fromFile(toAttach);
emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(emailIntent);
I used this tutorial and changed
messageBodyPart.setText(_body);
to
messageBodyPart.setContent(_body, "text/html");
I have a requirement that I need to attach a ".zip" file and send the email using Gmail Service.
I am using below code to do this:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType(application/x-compressed);
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{abc#gmail.com});
intent.putExtra(Intent.EXTRA_STREAM,
Uri.parse(abc.zip);
intent.putExtra(Intent.EXTRA_TEXT, "hello..");
If I use the "application/x-compressed" mime type , I am able to send ".zip" attachments but I am unable launch Gmail composer directly, before that it is providing list of options.
If I use "message/rfc822" mime type, I am able to launch Gmail composer directly, but unable to attach ".zip" files.
Pl. help me to how to combine these two mime types in a single intent object.
Pl. let me know if there's any alternative to do this.
thanks.
This worked for me -
intent.setType("application/zip, application/octet-stream, application/x-zip-compressed, multipart/x-zip")
Mime type found in this answer
intent.setClassName("com.google.android.gm", "com.google.android.gm.ComposeActivityGmail");
to open Gmail directly. however if gmail is not installed it will cause exception, ActivityNotFound
I am using this code and works. check this:
Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_EMAIL, new String[]{"Example#gmail.com"});
email.putExtra(Intent.EXTRA_SUBJECT, "subject//##");
email.putExtra(Intent.EXTRA_TEXT, "message//##");
email.setType("message/rfc822");
Uri uri = Uri.parse("sdcard/1.zip");
email.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(email, "Choose an Email client :"));
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 found a way to send plain text email using intent:
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("text/plain");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new
String[]{"example#mail.com"});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Test");
But I need to send HTML formatted text.
Trying to setType("text/html") doesn't work.
You can pass Spanned text in your extra. To ensure that the intent resolves only to activities that handle email (e.g. Gmail and Email apps), you can use ACTION_SENDTO with a Uri beginning with the mailto scheme. This will also work if you don't know the recipient beforehand:
final Intent shareIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:"));
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "The Subject");
shareIntent.putExtra(
Intent.EXTRA_TEXT,
Html.fromHtml(new StringBuilder()
.append("<p><b>Some Content</b></p>")
.append("<small><p>More content</p></small>")
.toString())
);
Been trying to send html via gmail app for a while, so decided to leave some insight on what I found, just in case someone else is having similar issues.
Seems like no matter what I did, I couldn't get the html to have bold text in it.
Then I've tried switching to outlook client and to my surprise it was working just fine.
Html markup was also working on other older devices, but not on mine (galaxy s7 API 26), so I figured, that gmail app seems to have dropped support for html syntax that comes from intent or maybe now you're required to provide it in some very specific way which is not clearly documented.
Last gmail version that worked for me was version 6.9.25... on Nexus 5X API 25 emulator (Nougat)
And it stopped working starting version 7.5.21... On Nexus 5x API 26 emulator (Oreo)
This was very helpful to me for the HTML, but the ACTION_SENDTO didn't quite work for me as is - I got an "action not supported" message. I found a variant here which does:
http://www.coderanch.com/t/520651/Android/Mobile/no-application-perform-action-when
And here's my code which combines the two together:
String mailId="yourmail#gmail.com";
Intent emailIntent = new Intent(Intent.ACTION_SENDTO,
Uri.fromParts("mailto",mailId, null));
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject text here");
// you can use simple text like this
// emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,"Body text here");
// or get fancy with HTML like this
emailIntent.putExtra(
Intent.EXTRA_TEXT,
Html.fromHtml(new StringBuilder()
.append("<p><b>Some Content</b></p>")
.append("<a>http://www.google.com</a>")
.append("<small><p>More content</p></small>")
.toString())
);
startActivity(Intent.createChooser(emailIntent, "Send email..."));
I haven't (yet) started Android development, but the documentation for the intent says that if you use EXTRA_TEXT, the MIME type should be text/plain. Seems like if you want to see HTML, you'd have to use EXTRA_STREAM instead...
You must to change "EXTRA_TEXT" for "EXTRA_HTML_TEXT"
https://developer.android.com/reference/android/content/Intent.html#EXTRA_HTML_TEXT
What about just trying to add some html in the text area?
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "<strong>Test</strong>");