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!
Related
Is it possible to send an email, using an intent, with an image in the body.
I'm using an image that is hosted...
public void sendEmail(View view){
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/html");
intent.putExtra(Intent.EXTRA_SUBJECT, "Image in body test");
intent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(getHtmlBody()));
startActivity(Intent.createChooser(intent, "Send Email"));
}
private String getHtmlBody() {
String html = "<h1><img width=\"100\" src=\"http://cdn2-www.dogtime.com/assets/uploads/gallery/30-impossibly-cute-puppies/impossibly-cute-puppy-8.jpg\"> Hello World </h1>";
return html;
}
I am able to do this using javaMail but that sends the email automatically without the user being able to see anything so I'm hoping I can use intents.
There is EXTRA_HTML_TEXT that allows you to supply HTML as alternative to the text in EXTRA_TEXT. However, there's no guarantee that the receiving app supports this extra (hence the requirement that EXTRA_TEXT must be present, too).
The code could look something like this:
public void sendEmail(View view){
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "Image in body test");
intent.putExtra(Intent.EXTRA_TEXT, getTextBody());
intent.putExtra(Intent.EXTRA_HTML_TEXT, getHtmlBody());
startActivity(Intent.createChooser(intent, "Send Email"));
}
private String getHtmlBody() {
return "<h1><img width=\"100\" src=\"http://cdn2-www.dogtime.com/assets/uploads/gallery/30-impossibly-cute-puppies/impossibly-cute-puppy-8.jpg\">"
+ getTextBody() + "</h1>";
}
private String getTextBody() {
return "Hello world";
}
Apps that don't support HTML will simply use the text from EXTRA_TEXT.
The answer by Arpit Garg in this post shares that and tags do not work in most email clients, so unfortunately your code wont work. I just tested your code with the Gmail app and a few others and it pulled in a placeholder but not the actual image. what you can do though is add an image as an attachment.
In my app I have it so that the user can send an email (code below).
This works, but I would like to improve it. There are 2 possibilities:
1. Currently, the url appears as a String. Can it be made to appear as a link in the email?
2. Include an image in the email (mylogo.png) and clicking on the image would go to the url
Is either of these possible?
private void sendEmail() {
final UserInfo userInfo = UserInfo.getInstance();
final String highScore = userInfo.getCumulativeScore();
final Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("*/*");
//intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(crashLogFile)); // attach a file
// EXTRA_EMAIL is the recipient, which in this case we don't know, so leave blank and let user fill in
//intent.putExtra(Intent.EXTRA_EMAIL, new String[] {"Extra Email"});
intent.putExtra(Intent.EXTRA_SUBJECT, "Super Quiz High Score");
String url = "https://play.google.com/store/apps/details?id=com.devname.appname";
intent.putExtra(Intent.EXTRA_TEXT, "My high score in the Super Quiz is now " + highScore + "!\n\n" + url);
startActivity(createEmailOnlyChooserIntent(intent, "Send via email"));
}
Depends completely on the mail client that the receiver uses. If it's gmail, you're out of luck, they don't parse links.
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.
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 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");