I'm trying to send an email after a survey is done. However when I try and use this line
startActivity(Intent.createChooser(emailIntent, "Email Reference Number"));
The dialog pops up that says "Email Reference Number"but below it says "No apps can perform this action". I'm using a Nexus 7 and I have gmail set up.
Is there a better way to bring up the option to choose an e-mail?
Thanks
Just in case here is the full email code
Intent emailIntent = new Intent(android.content.Intent.ACTION_SENDTO);
emailIntent.setType("message/rfc822");
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Reference Number");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml("<b>Thank you for your business, here is your reference number: " + ref + "</b>"));
startActivity(Intent.createChooser(emailIntent,"Email Reference Number"));
This works on my Nexus 7:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("message/rfc822");
intent.putExtra(Intent.EXTRA_SUBJECT, "Reference Number");
intent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml("<b>Thank you for your business, here is your reference number: " + ref + "</b>"));
startActivity(Intent.createChooser(intent,"Email Reference Number"));
If you use Intent.ACTION_SENDTO, you need to call setData() to set an appropriate mailto: URI.
You can also use the Intent.ACTION_SEND action and specify the recipient with the Intent.EXTRA_EMAIL extra.
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 using this code to use email application from my app.
String mailText = "Full Name:" + fname.getText().toString();
String subject = "Support";
Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_EMAIL, new String[] { "support#roncocala.com" });
email.putExtra(Intent.EXTRA_SUBJECT, subject);
email.setType("plain/text");
email.putExtra(Intent.EXTRA_TEXT, mailText);
startActivity(Intent.createChooser(email, "Choose an Email client :"));
But it shows extra applications like SKype and ES File Lan . Is there a way to limit these application to mail applications like gmail,yahoo,hotmail. Please help.Thanks.
To get only email client you need to use android.content.Intent.ACTION_SENDTO :
new Intent(Intent.ACTION_SENDTO); // return only the list of e-mail clients
you need to have configured an email account on those email client app or you'll have the error : "No application can perform this action".
ACTION_SENDTO only seems to be working for newer OS (at least API LEVEL 17+).
Unfortunately, this is the "best" current way of limiting the application list if you want to support older Android OS.
emailIntent.setType("message/rfc822");
None of the above solutions worked for me. After a lot of searching and testing, I finally found a good solution. Thanks to the Open source developer, cketti for sharing his/her concise and neat solution.
String mailto = "mailto:bob#example.org" +
"?cc=" + "alice#example.com" +
"&subject=" + Uri.encode(subject) +
"&body=" + Uri.encode(bodyText);
Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
emailIntent.setData(Uri.parse(mailto));
try {
startActivity(emailIntent);
} catch (ActivityNotFoundException e) {
//TODO: Handle case where no email app is available
}
And this is the link to his/her gist.
I'm trying to make a app, that takes information of some sort, then i want it to email that information to my gmail. I have found working code but when i load it onto my phone and run it and got all the info loaded into the app,and click the email, from what i understand its suppose to filter apps(on my phone) that are capable to send the email but I'm not getting anything, even though i have the default Email app that comes on the phone and i have Gmail.
public void Done(View view) {
Intent email = new Intent(Intent.ACTION_SENDTO); // it's not ACTION_SEND
email.putExtra(Intent.EXTRA_EMAIL, "some#gmail.com");
email.putExtra(Intent.EXTRA_SUBJECT, "OverStock Changes");
email.putExtra(Intent.EXTRA_TEXT, printReport());
email.setType("message/rfc822");
startActivity(Intent.createChooser(email, "Email"));
}
See answer for ACTION_SENDTO for sending an email
If you use ACTION_SENDTO, putExtra() does not work to add subject and
text to the intent. Use setData() and the Uri tool add subject and
text.
This example works for me:
// ACTION_SENDTO filters for email apps (discard bluetooth and others)
String uriText =
"mailto:youremail#gmail.com" +
"?subject=" + URLEncoder.encode("some subject text here") +
"&body=" + URLEncoder.encode("some text here");
Uri uri = Uri.parse(uriText);
Intent sendIntent = new Intent(Intent.ACTION_SENDTO);
sendIntent.setData(uri);
startActivity(Intent.createChooser(sendIntent, "Send email"));
Otherwise use ACTION_SEND as mentioned:
intent.setAction(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"mail#mail.com","mail2#mail.com"});
intent.putExtra(Intent.EXTRA_SUBJECT,"subject");
intent.putExtra(Intent.EXTRA_TEXT, "mail content");
startActivity(Intent.createChooser(intent, "title of dialog"));
I am sending an email in action view, it works perfectly fine in gmail , but if the user chooses any other mailing service it replaces spaces with '+'
like in body text is "check out it is a good day"
it displays as "check+out+it+is+a+good+day"
Any idea how to solve this issues
Here is my function for sending email
private void sendToAFriend() {
String subject = "it is a good day ";
String body = "Check out it is a good day";
String uriText =
"mailto:" +
"?subject=" + URLEncoder.encode(subject) +
"&body=" + URLEncoder.encode(body);
Uri uri = Uri.parse(uriText);
Intent sendIntent = new Intent(Intent.ACTION_SENDTO);
sendIntent.setData(uri);
startActivity(Intent.createChooser(sendIntent, "Send email"));
}
Try this code.
Intent intent = new Intent(Intent.ACTION_SENDTO); // it's not ACTION_SEND
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "Subject of email");
intent.putExtra(Intent.EXTRA_TEXT, "Body of email");
intent.setData(Uri.parse("mailto:default#recipient.com")); // or just "mailto:" for blank
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // this will make such that when user returns to your app, your app is displayed, instead of the email app.
startActivity(intent);
From the description of the method URLEncoder.encode
java.net.URLEncoder.encode(String s)
Deprecated. use encode(String, String) instead.
Encodes a given string s in a x-www-form-urlencoded string using the specified encoding scheme enc.
All characters except letters ('a'..'z', 'A'..'Z') and numbers ('0'..'9') and characters '.', '-', '*', '_' are converted into their hexadecimal value prepended by '%'. For example: '#' -> %23. In addition, spaces are substituted by '+'
Use Uri.encode(String) instead of the URLEncoder, it handles spaces correctly for this use case.
ACTION_VIEW with mailto link is more preferable if you wish to limit the sending options to email only.
Just use without any encode.
"&body=" + body;
it works for me!
I have the following problem:
I am sending an email via an intent and in the email I want to have linebreaks.
When I try "setType('text/plain')" and use \n's the Email-App doesn't use these, but the Gmail-App is OK.
When I set "setType('text/html')" and use br's and Html.fromHtml(emailtext), the Email-app doesn't do line breaks. When I am not using Html.fromHtml(emailtext) the Email-app makes linebreaks, but the Gmail-App displays the br's as normal text.
Isn't there a way to do simple linebreaks in Android email intents?
hi, may b this code will help u out
String emailMessage = "<html><body><div align='left'><p>I found this information on Actor Genie and wanted to share it with you.</p><p><b>Feature </b>: "
+ filmName
+ "</p><p><b>Casting Director</b>: "
+ casting
+ "</p><p><b>Distributor</b>: "
+ distributor
+ "<p><b>Story Line</b>: "
+ storyLine
+ "</p><p>For up to the date casting info get <a href='http://www.actorgenie.com/'>Actor Genie</a></p></div></body></html>";
Intent emailIntent = new Intent(
android.content.Intent.ACTION_SEND);
// emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, "");
emailIntent.setType("text/html");
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
emailSubject);
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, Html
.fromHtml(emailMessage));
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
finish();
Force Gmail by this
Intent emailIntent = new Intent(Intent.ACTION_VIEW);
emailIntent.setClassName("com.google.android.gm",
"com.google.android.gm.ComposeActivityGmail");
startActivity(emailIntent);
Use \n in your text code.
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Hello \n World");