android - 使用 `mailto:` 方案通过 Intent 发送电子邮件附件

标签 android email gmail

我正在使用此代码附加文件:

final Intent emailIntent = new Intent(android.content.Intent.ACTION_SENDTO);
String uriText;
Uri file = Uri.fromFile(new File(path));
uriText = "mailto:" + 
              "?subject=the subject" + 
              "&body=the body of the message"+
              "&attachment="+file;
uriText = uriText.replace(" ", "%20");
Uri uri = Uri.parse(uriText);
emailIntent.setData(uri);
startActivity(Intent.createChooser(emailIntent, "Send mail..."));

(请注意,path 类似于“/sdcard/test.jpg”,我使用了 ACTION_SENDTO,因为我只想在选择器中查看电子邮件应用程序.)

Intent 将提供电子邮件应用程序列表,但附件不会出现在电子邮件或 Gmail 中。如何让附件显示?

最佳答案

这似乎适用于我的 Galaxy Nexus 和 Nexus 4(均运行标准的 JellyBean API 17)。

具体来说:

Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto", "email@me.com", null));
intent.putExtra(android.content.Intent.EXTRA_STREAM, Uri.fromFile(attachmentFile));
startActivity(Intent.createChooser(intent, "Send email..."));

这不适用于我的 Nexus One (Gingerbread API 10) 或更旧的设备。我不确定它是从什么时候开始起作用的。

也许其他人对此有更多详细信息?

当 ACTION_SENDTO 不合适时:

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("vnd.android.cursor.dir/email");
intent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] { "email@me.com" });
intent.putExtra(android.content.Intent.EXTRA_STREAM, Uri.fromFile(attachmentFile));

关于android - 使用 `mailto:` 方案通过 Intent 发送电子邮件附件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12741865/

相关文章:

android - 从自定义 Fragment 调用 DialogFragment,然后设置其属性

android - Android 操作系统代码库中的 ActiveSync/MS 交换源在哪里

java - 从字符串中解析 JSON 数组

javascript - 通过 python 脚本发送包含 javascript 的动态 html 电子邮件

ios - 如何使用 iPhone 的邮件附件文件

javascript - 通过用 Electron/React/TS 编写的应用程序发送电子邮件

c# - SMTP 服务器需要安全连接 - gmail

javascript - 动态分配的 ID

android - Android WebView 缓存目录中的文件格式是什么(data_1、f_000001 等)?

java - Android 从资源中获取颜色列表