Send Email Intent
1 2 3 4 5 6 7 | Intent intent = new Intent(Intent.ACTION_SEND); intent.setType("text/html"); intent.putExtra(Intent.EXTRA_EMAIL,"[email protected]"); intent.putExtra(Intent.EXTRA_SUBJECT,"Subject"); intent.putExtra(Intent.EXTRA_TEXT,"I'm email body."); startActivity(Intent.createChooser(intent,"Send Email")); |
上面的代码打开一个显示以下应用程序的对话框:蓝牙、谷歌文档、雅虎邮箱、Gmail、Orkut、Skype等。
实际上,我想过滤这些列表选项。我只想显示与电子邮件相关的应用程序,如Gmail、Yahoo Mail。怎么做?
我在"android market"应用程序上看到过这样的例子。
该对话框仅显示电子邮件应用程序,如Gmail、Yahoo Mail等。它不显示蓝牙、Orkut等。是什么代码生成这样的对话框?
接受的答案不适用于4.1.2。这应该适用于所有平台:
1 2 3 4 5 | Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts( "mailto","[email protected]", null)); emailIntent.putExtra(Intent.EXTRA_SUBJECT,"Subject"); emailIntent.putExtra(Intent.EXTRA_TEXT,"Body"); startActivity(Intent.createChooser(emailIntent,"Send email...")); |
。
希望这有帮助。
更新:根据marcwjj的说法,在4.3版中,我们需要传递字符串数组,而不是电子邮件地址的字符串,以使其正常工作。我们可能需要再添加一行:
1 | intent.putExtra(Intent.EXTRA_EMAIL, addresses); // String[] addresses |
参考链接
主要有三种方法:
1 2 3 4 | String email = /* Your email address here */ String subject = /* Your subject here */ String body = /* Your body here */ String chooserTitle = /* Your chooser title here */ |
。
1。海关
1 2 3 4 5 6 7 8 | Uri uri = Uri.parse("mailto:" + email) .buildUpon() .appendQueryParameter("subject", subject) .appendQueryParameter("body", body) .build(); Intent emailIntent = new Intent(Intent.ACTION_SENDTO, uri); startActivity(Intent.createChooser(emailIntent, chooserTitle)); |
。
2。使用
1 2 3 4 5 6 | Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:" + email)); emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject); emailIntent.putExtra(Intent.EXTRA_TEXT, body); //emailIntent.putExtra(Intent.EXTRA_HTML_TEXT, body); //If you are using HTML in your body text startActivity(Intent.createChooser(emailIntent,"Chooser Title")); |
三。支持库
1 2 3 4 5 6 7 8 9 10 | Activity activity = /* Your activity here */ ShareCompat.IntentBuilder.from(activity) .setType("message/rfc822") .addEmailTo(email) .setSubject(subject) .setText(body) //.setHtmlText(body) //If you are using HTML in your body text .setChooserTitle(chooserTitle) .startChooser(); |
。
当你改变你的意图.settype,如下所示,你会得到
1 | intent.setType("text/plain"); |
使用
1 | new Intent(Intent.ACTION_SENDTO); |
号
我不建议你直接进入电子邮件应用程序。让用户选择他最喜欢的电子邮件应用程序。不要拘束他。
如果您使用action_sendto,PutExtra将无法向意图添加主题和文本。使用uri添加主题和正文文本。
编辑:我们可以使用
这是从android官方文件引用的,我已经在android 4.4上测试过了,并且工作得很好。更多示例请访问https://developer.android.com/guide/components/intents common.html电子邮件
1 2 3 4 5 6 7 8 9 | public void composeEmail(String[] addresses, String subject) { Intent intent = new Intent(Intent.ACTION_SENDTO); intent.setData(Uri.parse("mailto:")); // only email apps should handle this intent.putExtra(Intent.EXTRA_EMAIL, addresses); intent.putExtra(Intent.EXTRA_SUBJECT, subject); if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } } |
号
虽然我找到了一个可以帮助其他人的解决方案,但回答得晚了:
1 2 3 | Intent emailIntent = new Intent(Intent.ACTION_SENDTO); emailIntent.setData(Uri.parse("mailto:[email protected]")); startActivity(Intent.createChooser(emailIntent,"Send feedback")); |
这是我的输出(仅建议Gmail+收件箱):
。
我从Android开发者网站上得到了这个解决方案。
尝试:
1 | intent.setType("message/rfc822"); |
这对我很有用:
1 2 3 4 5 6 | Intent intent = new Intent(Intent.ACTION_SENDTO); intent.setData(Uri.parse("mailto:")); intent.putExtra(Intent.EXTRA_EMAIL , new String[] {"[email protected]" }); intent.putExtra(Intent.EXTRA_SUBJECT,"My subject"); startActivity(Intent.createChooser(intent,"Email via...")); |
号
即使用
这是根据android开发者文档来实现的方法。将这些代码行添加到应用程序中:
1 2 | Intent intent = new Intent(Intent.ACTION_SEND);//common intent intent.setData(Uri.parse("mailto:")); // only email apps should handle this |
号
如果要添加正文和主题,请添加此
1 2 | intent.putExtra(Intent.EXTRA_SUBJECT,"Your Subject Here"); intent.putExtra(Intent.EXTRA_TEXT,"E-mail body" ); |
号
如果只需要电子邮件客户机,则应使用带数组的
1 2 3 4 5 | final Intent result = new Intent(android.content.Intent.ACTION_SEND); result.setType("plain/text"); result.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] { recipient }); result.putExtra(android.content.Intent.EXTRA_SUBJECT, subject); result.putExtra(android.content.Intent.EXTRA_TEXT, body); |
号
最后想出最好的办法
1 2 3 4 5 6 7 8 9 | String to ="[email protected]"; String subject="Hi I am subject"; String body="Hi I am test body"; String mailTo ="mailto:" + to + "?&subject=" + Uri.encode(subject) + "&body=" + Uri.encode(body); Intent emailIntent = new Intent(Intent.ACTION_VIEW); emailIntent.setData(Uri.parse(mailTo)); startActivity(emailIntent); |
以下代码对我很有用。
1 2 3 4 5 6 | Intent intent = new Intent(Intent.ACTION_SEND); intent.setType("message/rfc822"); intent.putExtra(Intent.EXTRA_SUBJECT, subject); intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"abc@gmailcom"}); Intent mailer = Intent.createChooser(intent, null); startActivity(mailer); |
。
编辑:不再使用新版本的Gmail
这是我当时唯一能让它和任何角色一起工作的方法。
Doreamon的答案是正确的,因为它适用于Gmail新版本中的所有字符。
旧答案:
这是我的。它似乎适用于所有Android版本,支持主题和消息正文,支持完整的UTF-8字符:
1 2 3 4 5 6 7 8 9 10 11 12 | public static void email(Context context, String to, String subject, String body) { StringBuilder builder = new StringBuilder("mailto:" + Uri.encode(to)); if (subject != null) { builder.append("?subject=" + Uri.encode(Uri.encode(subject))); if (body != null) { builder.append("&body=" + Uri.encode(Uri.encode(body))); } } String uri = builder.toString(); Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse(uri)); context.startActivity(intent); } |
适用于所有Android版本:
1 2 3 4 5 6 7 8 9 | String[] TO = {"[email protected]"}; Uri uri = Uri.parse("mailto:[email protected]") .buildUpon() .appendQueryParameter("subject","subject") .appendQueryParameter("body","body") .build(); Intent emailIntent = new Intent(Intent.ACTION_SENDTO, uri); emailIntent.putExtra(Intent.EXTRA_EMAIL, TO); startActivity(Intent.createChooser(emailIntent,"Send mail...")); |
。
这些解决方案都不适合我。这是一个对棒棒糖有效的最小解决方案。在我的设备上,只有Gmail和本地电子邮件应用程序出现在结果选择器列表中。
1 2 3 4 5 6 | Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:" + Uri.encode(address))); emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject); emailIntent.putExtra(Intent.EXTRA_TEXT, body); startActivity(Intent.createChooser(emailIntent,"Send email via...")); |
号
这对我来说非常好:
1 2 3 | Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse("mailto:" + address)); startActivity(Intent.createChooser(intent,"E-mail")); |
号
以下代码对我有效!!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | import android.support.v4.app.ShareCompat; . . . . final Intent intent = ShareCompat.IntentBuilder .from(activity) .setType("application/txt") .setSubject(subject) .setText("Hii") .setChooserTitle("Select One") .createChooserIntent() .addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); activity.startActivity(intent); |
号
这些答案中的大多数只适用于不发送附件的简单情况。在我的情况下,有时我需要发送附件(action_send)或两个附件(action_send_multiple)。
所以我从这条线索中选择了最好的方法,并将它们结合起来。它使用的是支持库的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | fun Activity.sendEmail(recipients: List<String>, subject: String, file: Uri, text: String? = null, secondFile: Uri? = null) { val originalIntent = createEmailShareIntent(recipients, subject, file, text, secondFile) val emailFilterIntent = Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:")) val originalIntentResults = packageManager.queryIntentActivities(originalIntent, 0) val emailFilterIntentResults = packageManager.queryIntentActivities(emailFilterIntent, 0) val targetedIntents = originalIntentResults .filter { originalResult -> emailFilterIntentResults.any { originalResult.activityInfo.packageName == it.activityInfo.packageName } } .map { createEmailShareIntent(recipients, subject, file, text, secondFile).apply { `package` = it.activityInfo.packageName } } .toMutableList() val finalIntent = Intent.createChooser(targetedIntents.removeAt(0), R.string.choose_email_app.toText()) finalIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedIntents.toTypedArray()) startActivity(finalIntent) } private fun Activity.createEmailShareIntent(recipients: List<String>, subject: String, file: Uri, text: String? = null, secondFile: Uri? = null): Intent { val builder = ShareCompat.IntentBuilder.from(this) .setType("message/rfc822") .setEmailTo(recipients.toTypedArray()) .setStream(file) .setSubject(subject) if (secondFile != null) { builder.addStream(secondFile) } if (text != null) { builder.setText(text) } return builder.intent } |
如果您想确保您的意图仅由电子邮件应用程序(而不是其他短信或社交应用程序)处理,则使用
1 2 3 4 5 6 7 8 9 | public void composeEmail(String[] addresses, String subject) { Intent intent = new Intent(Intent.ACTION_SENDTO); intent.setData(Uri.parse("mailto:")); // only email apps should handle this intent.putExtra(Intent.EXTRA_EMAIL, addresses); intent.putExtra(Intent.EXTRA_SUBJECT, subject); if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } } |
号
我在https://developer.android.com/guide/components/intents common.html电子邮件中找到了这个。
如果你想以Gmail为目标,那么你可以做以下的事情。请注意,目的是"actionu sendto",而不是"actionu send",并且gmail不需要额外的目的字段。
1 2 3 4 5 6 7 8 9 10 11 12 | String uriText = "mailto:[email protected]" + "?subject=" + Uri.encode("your subject line here") + "&body=" + Uri.encode("message body here"); Uri uri = Uri.parse(uriText); Intent sendIntent = new Intent(Intent.ACTION_SENDTO); sendIntent.setData(uri); if (sendIntent.resolveActivity(getPackageManager()) != null) { startActivity(Intent.createChooser(sendIntent,"Send message")); } |
。
也许你应该试试这个:
我在这里找到的。我在我的应用程序中使用过它,它只显示电子邮件和Gmail选项。
This code is working in my device
号
1 2 3 4 5 | Intent mIntent = new Intent(Intent.ACTION_SENDTO); mIntent.setData(Uri.parse("mailto:")); mIntent.putExtra(Intent.EXTRA_EMAIL , new String[] {"[email protected]"}); mIntent.putExtra(Intent.EXTRA_SUBJECT,""); startActivity(Intent.createChooser(mIntent,"Send Email Using...")); |
使用此:
1 2 3 4 5 6 | boolean success = EmailIntentBuilder.from(activity) .to("[email protected]") .cc("[email protected]") .subject("Error report") .body(buildErrorReport()) .start(); |
号
使用生成渐变:
1 | compile 'de.cketti.mailto:email-intent-builder:1.0.0' |
号
这就是我所用的,它对我有用:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | //variables String subject ="Whatever subject you want"; String body ="Whatever text you want to put in the body"; String intentType ="text/html"; String mailToParse ="mailto:"; //start Intent Intent variableName = new Intent(Intent.ACTION_SENDTO); variableName.setType(intentType); variableName.setData(Uri.parse(mailToParse)); variableName.putExtra(Intent.EXTRA_SUBJECT, subject); variableName.putExtra(Intent.EXTRA_TEXT, body); startActivity(variableName); |
这也将允许用户选择他们喜欢的电子邮件应用程序。唯一不允许你做的就是设置收件人的电子邮件地址。
在科特林如果有人在找
1 2 3 4 5 6 7 8 | val emailArrray:Array<String> = arrayOf("[email protected]") val intent = Intent(Intent.ACTION_SENDTO) intent.data = Uri.parse("mailto:") // only email apps should handle this intent.putExtra(Intent.EXTRA_EMAIL, emailArrray) intent.putExtra(Intent.EXTRA_SUBJECT,"Inquire about travel agent") if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } |
号
我在更新阿迪尔在科特林的答案,
1 2 3 4 5 6 7 8 9 | val intent = Intent(Intent.ACTION_SENDTO) intent.data = Uri.parse("mailto:") // only email apps should handle this intent.putExtra(Intent.EXTRA_EMAIL, Array(1) {"[email protected]" }) intent.putExtra(Intent.EXTRA_SUBJECT,"subject") if (intent.resolveActivity(packageManager) != null) { startActivity(intent) } else { showSnackBar(getString(R.string.no_apps_found_to_send_mail), this) } |
。
来自Android开发者文档:
1 2 3 4 5 6 7 8 9 | public void composeEmail(String[] addresses, String subject) { Intent intent = new Intent(Intent.ACTION_SENDTO); intent.setData(Uri.parse("mailto:")); // only email apps should handle this intent.putExtra(Intent.EXTRA_EMAIL, addresses); intent.putExtra(Intent.EXTRA_SUBJECT, subject); if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } } |
在电话电子邮件客户端撰写电子邮件:
1 2 3 4 5 6 | Intent intent = new Intent(Intent.ACTION_SEND); intent.setType("plain/text"); intent.putExtra(Intent.EXTRA_EMAIL, new String[] {"[email protected]" }); intent.putExtra(Intent.EXTRA_SUBJECT,"subject"); intent.putExtra(Intent.EXTRA_TEXT,"mail body"); startActivity(Intent.createChooser(intent,"")); |
号
使用EDOCX1[1]确实有效,但它显示了不必处理电子邮件的额外应用程序(如gdrive)。使用
1 2 3 4 5 6 | Intent intent = new Intent(Intent.ACTION_SENDTO); intent.setType("text/plain"); intent.setData(Uri.parse("mailto:[email protected]")); intent.putExtra(Intent.EXTRA_SUBJECT,"Email from My app"); intent.putExtra(Intent.EXTRA_TEXT,"Place your email message here ..."); startActivity(Intent.createChooser(intent,"Send Email")); |
使用安科-科特林
1 | context.email(email, subject, body) |
。
1 2 3 4 5 6 7 | Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts( "mailto", email, null)); if (emailIntent.resolveActivity(context.getPackageManager()) != null) { context.startActivity(Intent.createChooser(emailIntent,"Send Email...")); } else { Toast.makeText(context,"No apps can perform this action.", Toast.LENGTH_SHORT).show(); } |
号
仅发送到电子邮件客户端-带有多个附件
有很多解决方案,但都是部分有效的。
mailto正确过滤电子邮件应用程序,但它无法不发送流/文件。
message/rfc822与电子邮件客户端一起打开地狱般的应用程序
所以,解决这个问题的方法是同时使用两者。
1 2 3 4 5 6 7 8 | private void share() { Intent queryIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:")); Intent dataIntent = getDataIntent(); Intent targetIntent = getSelectiveIntentChooser(context, queryIntent, dataIntent); startActivityForResult(targetIntent); } |
。
构建所需的数据意图,其中包含要共享的所需数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | private Intent getDataIntent() { Intent dataIntent = buildIntent(Intent.ACTION_SEND, null,"message/rfc822", null); // Set subject dataIntent.putExtra(Intent.EXTRA_SUBJECT, title); //Set receipient list. dataIntent.putExtra(Intent.EXTRA_EMAIL, toRecipients); dataIntent.putExtra(Intent.EXTRA_CC, ccRecipients); dataIntent.putExtra(Intent.EXTRA_BCC, bccRecipients); if (hasAttachments()) { ArrayList<Uri> uris = getAttachmentUriList(); if (uris.size() > 1) { intent.setAction(Intent.ACTION_SEND_MULTIPLE); dataIntent.putExtra(Intent.EXTRA_STREAM, uris); } else { dataIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris.get(0)); } } return dataIntent; } protected ArrayList<Uri> getAttachmentUriList() { ArrayList<Uri> uris = new ArrayList(); for (AttachmentInfo eachAttachment : attachments) { uris.add(eachAttachment.uri); } return uris; } |
用于根据查询意图筛选所需意图的utitlity类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | // Placed in IntentUtil.java public static Intent getSelectiveIntentChooser(Context context, Intent queryIntent, Intent dataIntent) { List<ResolveInfo> appList = context.getPackageManager().queryIntentActivities(queryIntent, PackageManager.MATCH_DEFAULT_ONLY); Intent finalIntent = null; if (!appList.isEmpty()) { List targetedIntents = new ArrayList(); for (ResolveInfo resolveInfo : appList) { String packageName = resolveInfo.activityInfo != null ? resolveInfo.activityInfo.packageName : null; Intent allowedIntent = new Intent(dataIntent); allowedIntent.setComponent(new ComponentName(packageName, resolveInfo.activityInfo.name)); allowedIntent.setPackage(packageName); targetedIntents.add(allowedIntent); } if (!targetedIntents.isEmpty()) { //Share Intent Intent startIntent = targetedIntents.remove(0); Intent chooserIntent = android.content.Intent.createChooser(startIntent,""); chooserIntent.putExtra(android.content.Intent.EXTRA_INITIAL_INTENTS, targetedIntents.toArray(new Parcelable[]{})); chooserIntent.addFlags(android.content.Intent.FLAG_GRANT_READ_URI_PERMISSION); finalIntent = chooserIntent; } } if (finalIntent == null) //As a fallback, we are using the sent data intent { finalIntent = dataIntent; } return finalIntent; } |
。