Android Studio mailto Intent 不显示主题和邮件正文



我正在尝试从我的安卓应用程序发送电子邮件。点击按钮,Gmail应该会打开并显示一封新电子邮件,其中包含我之前定义的收件人,主题和电子邮件正文。到目前为止,我已经尝试发送Intent.ACTION_VIEW和Intent.ACTION_SENDTO。两者都只向收件人显示我的草稿。主题和信息都被压制了。奇怪的是,当使用模拟器时,它工作得很好。还试图锁定安卓错误日志。好像我没有权限。这真的是权限问题还是其他问题? 我真的很感激任何帮助 干杯

这是我的代码:

  • 通过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);
  • 通过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 :"));
  • 来自日志猫的错误消息
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

我认为我们遇到了同样的问题。Android API 29 引入了一些关于将数据发送到其他应用的改进。在此处查看更多详细信息:将简单数据发送到其他应用

这是对我有用的解决方案。

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[]{"address@mail.com"});
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..."));

简而言之,您正在请求Android标准应用程序选择器,此外,您还指定要发送电子邮件。因此,因此,电子邮件客户端将仅显示。 如果用户只安装了一个电子邮件客户端,则意图将立即重定向到它。

希望这也对你有帮助。

试试这段代码,它对我有用。

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);
}

还要在安卓清单中添加意图过滤器。

<activity ...>
<intent-filter>
<action android:name="android.intent.action.SENDTO" />
<data android:scheme="mailto" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>

为了让它在三星和Pixel设备上工作,我们必须在url附加内容上添加参数。

val email = "xxxx@xxxx.com"
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"))

我们的电子邮件旧代码几天前停止工作。

具体如下:

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();
}
}

我根据Zak.Antonio的回答采用了它:

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();
}
}

关键点是:

  • emailIntent中将Intent.ACTION_SENDTO替换为Intent.ACTION_SEND
  • Intent.ACTION_SENDTO移动到selectorIntent
  • 不要将电子邮件放在意图数据中,而只将它们放在额外的内容中Intent.EXTRA_EMAIL

这个答案对我有用,使用 Uri 进行解析

Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:?subject=" + subject + "&to=" + to));
context.startActivity(emailIntent);

如果您的应用以Android 11(API 级别 30(或更高版本为目标平台,那么我们需要添加:

  • 应用清单文件中<queries>元素(因为 Android 11 中的软件包可见性更改(

在清单文件中设置queries,如下所示:

<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 打开电子邮件应用程序的方法:

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)
}
}

无论是在SENDTO意图中添加Extras还是使用选择器意图似乎都不适用于运行Android 10的三星。对电子邮件地址、主题和正文进行编码的 Uri 似乎在运行 Android 10 及更低版本的多个设备上效果最好。EmailIntentLibrary在解决使URI编码适用于复杂正文内容的细节方面提供了很大的帮助。

val email = Uri.encode("xxxx@xxxx.com")
val subject = Uri.encode("xxxx")
val body = Uri.encode("some body. one two & more. n new line n &%>?")
val uri = "mailto:$email?subject=$subject&body=$body"
val intent = Intent(Intent.ACTION_SENDTO)
intent.type = "text/plain"
intent.data = Uri.parse(uri)
startActivity(intent)

如果您对选择器中有很多选项感到满意,那么Android开发指南中的以下示例确实有效(用于过滤这些选项的ACTION_SENDTO示例对我不起作用(

fun composeEmail(addresses: Array<String>, subject: String, attachment: Uri) {
val intent = Intent(Intent.ACTION_SEND).apply {
type = "*/*"
putExtra(Intent.EXTRA_EMAIL, addresses)
putExtra(Intent.EXTRA_SUBJECT, subject)
putExtra(Intent.EXTRA_STREAM, attachment)
}
if (intent.resolveActivity(packageManager) != null) {
startActivity(intent)
}
}

试试这段代码

val emailIntent = Intent(Intent.ACTION_SEND)
emailIntent.setType("text/plain")
emailIntent.putExtra(Intent.EXTRA_EMAIL, arrayOf("jon@example.com")) 
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Email subject")
emailIntent.putExtra(Intent.EXTRA_TEXT, "Email message text")
val packageManager = packageManager
val activities = packageManager.queryIntentActivities(emailIntent, 0)
val isIntentSafe = activities.size > 0
if (isIntentSafe) {
startActivity(emailIntent);
}else{
Log.d("MainActivty","Email App not installed");
}

我对其他解决方案有问题,以下是对我有用的解决方案:

val selectorIntent = Intent(Intent.ACTION_SENDTO).apply {
data = Uri.parse("mailto:")
}
val emailIntent = Intent(Intent.ACTION_SEND).apply {
selector = selectorIntent
putExtra(
Intent.EXTRA_EMAIL,
arrayOf(resources.getString(R.string.support_email_address))
)
putExtra(
Intent.EXTRA_SUBJECT,
resources.getString(R.string.support_email_subject)
)
putExtra(
Intent.EXTRA_TEXT,
resources.getString(R.string.support_email_body)
)
putExtra(
Intent.EXTRA_STREAM,
latestLogFileUri
)
}
emailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
if (selectorIntent.resolveActivity(packageManager) != null) {
Handler(Looper.getMainLooper()).postDelayed({
startActivity(
Intent.createChooser(
emailIntent,
resources.getString(R.string.support_email_chooser_title)
)
)
}, 2000)
}

并且不要忘记将其放入您的AndroidManifest.xml中:

<queries>
<intent>
<action android:name="android.intent.action.SENDTO" />
<data android:scheme="mailto" />
</intent>
<intent>
<action android:name="android.intent.action.SEND" />
<data android:mimeType="*/*" />
</intent>
</queries>

此问题的最佳解决方案

val intent = Intent(Intent.ACTION_SENDTO)
intent.data = Uri.parse("mailto:example@gmail.com?bcc=exampleEmail2@gmail.com&subject=subject text...!&body= extra text ..")
startActivity(intent)

最新更新