Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio mailto Intent doesn't show subject and mail body

I'm trying to send an e-mail from my Android App. With the click on a button, gmail should open and show a new email with my previously defined recipient, subject and email body. So far I've tried sending the Intent.ACTION_VIEW as well as Intent.ACTION_SENDTO. Both show my draft with the recipient only. Both subject and message are being opressed. Weird thing is when using the emulator, it works just fine. Also was trying to lock at the android errorlog. Seems like i don't have permission. Is it really a permission problem or could it be something else? I'd really appreciate any help cheers

Here is my code:

  • sending email via ACTION_VIEW
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("mailto:" + to));
intent.putExtra(intent.EXTRA_SUBJECT, subject);
intent.putExtra(intent.EXTRA_TEXT, message);
mainActivity.startActivity(intent);
  • sending email via ACTION_SENDTO
Intent email = new Intent(Intent.ACTION_SENDTO);
email.setType("message/rfc822");
email.putExtra(Intent.EXTRA_EMAIL, new String[]{to});
email.putExtra(Intent.EXTRA_SUBJECT, subject);
email.putExtra(Intent.EXTRA_TEXT, message);
mainActivity.startActivity(Intent.createChooser(email, "Choose an Email client :"));
  • error message from logcat
2019-12-13 01:30:35.172 29268-29268/? E//system/bin/webview_zygote32: failed to make and chown /acct/uid_99044: Permission denied
2019-12-13 01:30:35.172 29268-29268/? E/Zygote: createProcessGroup(99044, 0) failed: Permission denied
2019-12-13 01:30:35.206 29289-29289/? E/asset: setgid: Operation not permitted
2019-12-13 01:30:35.226 29296-29296/? E/asset: setgid: Operation not permitted
2019-12-13 01:30:35.355 29268-29268/? E/Typeface: Error mapping font file /system/fonts/NotoSansKhmer-Regular.ttf
2019-12-13 01:30:35.356 29268-29268/? E/Typeface: Error mapping font file /system/fonts/NotoSansKhmer-Bold.ttf
2019-12-13 01:30:35.356 29268-29268/? E/Minikin: Could not get cmap table size!
2019-12-13 01:30:35.356 29268-29268/? E/Typeface: Unable to load Family: null:und-Khmr
2019-12-13 01:30:35.484 29268-29268/? E/Typeface: Error mapping font file /system/fonts/LGAka_Light.ttf
2019-12-13 01:30:35.484 29268-29268/? E/Minikin: Could not get cmap table size!
2019-12-13 01:30:35.484 29268-29268/? E/Typeface: Unable to load Family: lg-lgaka:null
2019-12-13 01:30:35.816 29342-29342/? E//system/bin/webview_zygote32: failed to make and chown /acct/uid_99045: Permission denied
2019-12-13 01:30:35.816 29342-29342/? E/Zygote: createProcessGroup(99045, 0) failed: Permission denied
2019-12-13 01:30:35.842 29354-29354/? E/asset: setgid: Operation not permitted
2019-12-13 01:30:35.864 29367-29367/? E/asset: setgid: Operation not permitted
2019-12-13 01:30:36.139 29342-29342/? E/Typeface: Error mapping font file /system/fonts/NotoSansKhmer-Regular.ttf
2019-12-13 01:30:36.139 29342-29342/? E/Typeface: Error mapping font file /system/fonts/NotoSansKhmer-Bold.ttf
2019-12-13 01:30:36.139 29342-29342/? E/Minikin: Could not get cmap table size!
2019-12-13 01:30:36.139 29342-29342/? E/Typeface: Unable to load Family: null:und-Khmr
2019-12-13 01:30:36.362 29342-29342/? E/Typeface: Error mapping font file /system/fonts/LGAka_Light.ttf
2019-12-13 01:30:36.362 29342-29342/? E/Minikin: Could not get cmap table size!
2019-12-13 01:30:36.362 29342-29342/? E/Typeface: Unable to load Family: lg-lgaka:null
2019-12-13 01:30:36.523 4349-4359/? E/GBMv2: FPS Scaler: EXP
2019-12-13 01:30:36.602 29342-29342/? E/WebViewFactory: can't load with relro file; address space not reserved
2019-12-13 01:30:37.058 29220-29220/? E/Gmail: Gmail:EditWebView JS Console: b/119949571:draft.editor.onLoad; source: file:///android_asset/draft_editor_gmail_compiled.js at 89
2019-12-13 01:30:37.146 29220-29220/? E/Gmail: Gmail:EditWebView JS Console: b/119949571:draft.editor.onLoad is finished; source: file:///android_asset/draft_editor_gmail_compiled.js at 90
like image 907
Paul Avatar asked Dec 13 '19 00:12

Paul


People also ask

Is intent deprecated?

The Intent is not deprecated the problem is with your itemClickable. class because it is not recognized.

What is Android intent Action view?

An intent allows you to start an activity in another app by describing a simple action you'd like to perform (such as "view a map" or "take a picture") in an Intent object.

What is the syntax of intent in android?

Intent in = new Intent(this, SecondActivity. class); You can't do this inside an OnClickListener because the first parameter needs to be a Context (or a class that extends Context , like Activity ).


6 Answers

I think we had the same issue. Android API 29 introduced some improvements about sending data to other apps. See more details here: Sending simple data to other apps

Here is the solution that works for me.

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 protected]"});
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "The subject");
emailIntent.putExtra(Intent.EXTRA_TEXT, "The email body");
emailIntent.setSelector( selectorIntent );

activity.startActivity(Intent.createChooser(emailIntent, "Send email..."));

In few words, with this you are asking for the Android standard app chooser and, in addition, you specify that you want to send an email. So, as result, email clients will appear only. If user has one email client installed only, the intent will redirect to it instantly.

Hope this helps you too.

like image 184
zak.antonio Avatar answered Oct 16 '22 11:10

zak.antonio


try out this code, it worked for me.

Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:")); // only email apps should handle this
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{email});
intent.putExtra(Intent.EXTRA_SUBJECT, "Subject here");
intent.putExtra(Intent.EXTRA_TEXT,"Body Here");
if (intent.resolveActivity(getPackageManager()) != null) {
    startActivity(intent);
}

also add intent filter in android manifest.

<activity ...>
<intent-filter>
    <action android:name="android.intent.action.SENDTO" />
    <data android:scheme="mailto" />
    <category android:name="android.intent.category.DEFAULT" />
</intent-filter>
like image 21
Siddharth Vaish Avatar answered Oct 16 '22 13:10

Siddharth Vaish


In order to get it to work on Samsung and Pixel devices, we had to add the parameters on both the url and the extras

val email = "[email protected]"
val subject = "xxxx"
val body = "xxxx"

val selectorIntent = Intent(Intent.ACTION_SENDTO)
val urlString = "mailto:" + Uri.encode(email) + "?subject=" + Uri.encode(subject) + "&body=" + Uri.encode(body)
selectorIntent.data = Uri.parse(urlString)

val emailIntent = Intent(Intent.ACTION_SEND)
emailIntent.putExtra(Intent.EXTRA_EMAIL, arrayOf(email))
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject)
emailIntent.putExtra(Intent.EXTRA_TEXT, body)
emailIntent.selector = selectorIntent

startActivity(Intent.createChooser(emailIntent, "Send email"))
like image 14
Fran Sevillano Avatar answered Oct 16 '22 13:10

Fran Sevillano


Our old code for emails stopped working some days ago.

It was the following:

public static void shareTextToEmail(Context context, String[] email, String subject, String text)
    Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:" + TextUtils.join(",", email)));
    emailIntent.putExtra(Intent.EXTRA_EMAIL, email);
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
    emailIntent.putExtra(Intent.EXTRA_TEXT, text);
    try {
        context.startActivity(Intent.createChooser(emailIntent, context.getString(R.string.share_email_title)));
    } catch (android.content.ActivityNotFoundException e) {
        Toast.makeText(context, context.getString(R.string.share_no_intent_handler_found), Toast.LENGTH_SHORT).show();
    }
}

I've adopted it according to the Zak.Antonio answer:

public static void shareTextToEmail(Context context, String[] email, String subject, String text)
    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, email);
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
    emailIntent.putExtra(Intent.EXTRA_TEXT, text);
    emailIntent.setSelector(selectorIntent);

    try {
        context.startActivity(Intent.createChooser(emailIntent, context.getString(R.string.share_email_title)));
    } catch (android.content.ActivityNotFoundException e) {
        Toast.makeText(context, context.getString(R.string.share_no_intent_handler_found), Toast.LENGTH_SHORT).show();
    }
}

The key points are:

  • Replace Intent.ACTION_SENDTO with Intent.ACTION_SEND in emailIntent
  • Move Intent.ACTION_SENDTO to a selectorIntent
  • Do not put emails in intent data, put them only in extras at Intent.EXTRA_EMAIL
like image 5
Artem Mostyaev Avatar answered Oct 16 '22 11:10

Artem Mostyaev


This answer worked for me using parsing using Uri

Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:?subject=" + subject + "&to=" + to));
 context.startActivity(emailIntent);
like image 3
Ahmed Elsayed Avatar answered Oct 16 '22 11:10

Ahmed Elsayed


If your app targets Android 11 (API level 30) or higher then we require to add :

  • <queries> element in the app's manifest file (Because of Package visibility changes in Android 11)

Set the queries in the Manifest file like this:

<manifest package="com.example.app">
    ...
    <!-- Package visibility -->
    <queries>
        <!-- Mail -->
        <intent>
            <action android:name="android.intent.action.SENDTO" />
            <data android:scheme="mailto" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent>
    </queries>
    ...
</manifest>

Kotlin method to open Email apps :

fun composeEmail(recipient: String, subject: String, body: String) {
    val selectorIntent = Intent(Intent.ACTION_SENDTO).apply{
        data = Uri.parse("mailto:") // only email apps should handle this
    }
    val emailIntent = Intent(Intent.ACTION_SEND).apply {
        putExtra(Intent.EXTRA_EMAIL, arrayOf(recipient))
        putExtra(Intent.EXTRA_SUBJECT, subject)
        putExtra(Intent.EXTRA_TEXT, body)
        selector = selectorIntent
    }
    if (emailIntent.resolveActivity(packageManager) != null) {
        startActivity(emailIntent)
    }
}
like image 3
Nikunj Avatar answered Oct 16 '22 13:10

Nikunj