6

I'm using this code:

Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto",email, null));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(Intent.EXTRA_TEXT, text);
activity.startActivity(Intent.createChooser(emailIntent, "Send feedback to xyz"));

for 2 years. And until now everything worked fine. User can select message client and send feedback with prefilled data inside. It worked fine for all mail clients. Recently noticed that if I select gmail client - body of message remains empty, but on other mail clients body is filled with text.

Any ideas?

Kostadin
  • 2,499
  • 5
  • 34
  • 58
  • 1
    Maybe this is same question and it looks solved. https://stackoverflow.com/questions/59314608/android-studio-mailto-intent-doesnt-show-subject-and-mail-body – kochizufan Jan 21 '20 at 08:38
  • 1
    I hate such problems, once it is working and afterwards it is not working whereas you did not changed anything. Great to see your answer thanks! – Berkay Turancı Feb 28 '20 at 14:41

6 Answers6

13

Thanks for help

Made tests with lots of suggested answers. adding "text/plain" or "message/rfc822" made my app to stop offering mail clients.

Fount this answer that fixed my issue: https://stackoverflow.com/a/59365539/973233

Most interesting part for me is having 2 intents:

Intent selectorIntent = new Intent(Intent.ACTION_SENDTO);
selectorIntent.setData(Uri.parse("mailto:"));
final Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{email});
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(Intent.EXTRA_TEXT, text);
emailIntent.setSelector( selectorIntent );
activity.startActivity(Intent.createChooser(emailIntent, "Send feedback to XYZ"));

This solved problem.

Kostadin
  • 2,499
  • 5
  • 34
  • 58
  • The email doesn't get sent this way! They seem to disappear, neither do they get shown in outbox nor sent mails – Hissaan Ali Sep 05 '20 at 05:31
  • I don't believe that adding `"text/plain"` for type can stop offering mail clients, it should have been something elese – user924 Oct 20 '20 at 16:21
  • Saved my day, thank you very much for this answer! I had the same issue when adding text/plain or message/rfc822 but this double-intent workaround works as expected. – Fernando Jascovich Apr 26 '21 at 15:14
1

Recently i encountered the same issue. While searching, i found this as being the best solution (kotlin) (at least for myself):

fun sendEmail(email: String, subject: String, message: String) {
    val emailIntent = Intent(Intent.ACTION_SEND)
    emailIntent.data = Uri.parse("mailto:")
    emailIntent.type = "text/plain"
    emailIntent.putExtra(Intent.EXTRA_EMAIL, arrayOf(email))
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject)
    emailIntent.putExtra(Intent.EXTRA_TEXT, message)

    val sendIntent = Intent.createChooser(emailIntent, "Please Choose Email Client...")
    sendIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK

    try {
        context.startActivity(sendIntent)
    } catch (e: Exception) {
        Toast.makeText(context, e.message, Toast.LENGTH_LONG).show()
    }
}
Mihai Neacsu
  • 245
  • 1
  • 3
  • 10
  • 1
    the line emailIntent.type = "text/plain" gives the error " *No apps can perform this action*. But when removed , everything works – Yonko Kilasi Feb 24 '20 at 08:34
1

I use body properties for gmail and EXTRA_TEXT for other email. I've tested for different email app such as samsung email, oneplus email, and LG email, they seems to support EXTRA_TEXT but gmail is supporting "body" properties.

 fun composeEmailMessage(context: Context,  subject: String, body: String, emails: Array<String> = arrayOf()) {
    val intent = Intent(Intent.ACTION_SENDTO)
    intent.data = Uri.parse("mailto:")
    intent.putExtra(Intent.EXTRA_EMAIL, emails)
    intent.putExtra(Intent.EXTRA_SUBJECT, subject)
    intent.putExtra(Intent.EXTRA_TEXT, body)//other emails app
    intent.putExtra("body", body)//gmail
    if (intent.resolveActivity(context.packageManager) != null) {
        context.startActivity(Intent.createChooser(intent, "Send email via..."))
    }
}
0

To send an email with a body, use message/rfc822.

Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("message/rfc822");
sendIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { "to1@example.com", "to2@example.com" });
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject of the email");
sendIntent.putExtra(Intent.EXTRA_TEXT, "Content of the email");
startActivity(sendIntent);

Hope this helps.

Kalpesh Rupani
  • 991
  • 4
  • 12
0

I'm using the following code and is working for every email client. Example:

Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
intent.setType("plain/text");
intent.putExtra(Intent.EXTRA_EMAIL, new String[] {"email id of receiver"});
intent.putExtra(Intent.EXTRA_SUBJECT, "This is the subject of the email client");
intent.putExtra(Intent.EXTRA_TEXT, "This is the body of the email client");

// this line is for attaching file
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);

intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
startActivity(Intent.createChooser(intent, "Send Email"));
Amit Jangid
  • 2,741
  • 1
  • 22
  • 28
0

I had similar issue. This worked for me: instead of

"mailto"

You have to use

"mailto:"

I have also replaced "Uri.fromParts" with "Uri.parse"

Marat K
  • 23
  • 5