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!

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top