Android email replaces space with plus (+)

I send an email as an action, it works fine in gmail, but if the user selects any other email service, it replaces the 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 these problems

Here is my email sending function

 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")); } 
+1
source share
4 answers

Try this code.

 Intent intent = new Intent(Intent.ACTION_SENDTO); // it 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); 
+4
source

From the description of the URLEncoder.encode method

java.net.URLEncoder.encode (String s) Deprecated. use encode (String, String) instead. Encodes the specified string s in the string x-www-form-urlencoded using the specified enc encoding scheme.

All characters except letters ('a' .. 'z', 'A' .. 'Z') and numbers ('0' .. '9') and the characters '.', '-', '*', '_' are converted to their hexadecimal value added by '%'. For example: '#' →% 23. In addition, spaces are replaced with "+"

+1
source

Use Uri.encode (String) instead of URLEncoder, it handles spaces correctly for this use case. ACTION_VIEW with a mailto link is preferable if you want to limit the ability to send only by email.

0
source

Just use without coding.

"& body =" + body;

he works for me!

0
source

Source: https://habr.com/ru/post/976777/


All Articles