Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/html");
intent.putExtra(Intent.EXTRA_EMAIL, "emailaddress@emailaddress.com");
intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
intent.putExtra(Intent.EXTRA_TEXT, "I'm email body.");
startActivity(Intent.createChooser(intent, "Send Email"));
上面的代码打开一个对话框,显示以下应用程序:蓝牙、Google Docs、Yahoo Mail、Gmail、Orkut、Skype 等。
实际上,我想过滤这些列表选项。我只想显示与电子邮件相关的应用程序,例如 Gmail 和 Yahoo Mail。怎么做?
我在“Android Market 应用程序”中看到过这样的示例。
打开 Android Market 应用程序 打开开发者指定其电子邮件地址的任何应用程序。 (如果您找不到这样的应用程序,只需打开我的应用程序:-market://details?id=com.becomputer06.vehicle.diary.free,或通过“车辆日记”搜索)向下滚动到“开发人员”点击'发送电子邮件'
该对话框仅显示电子邮件应用程序,例如 Gmail、Yahoo Mail 等。它不显示蓝牙、Orkut 等。什么代码产生这样的对话框?
String[]
,而不仅仅是此处所示的 String
。
更新
官方做法:
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);
}
}
旧答案
接受的答案不适用于 4.1.2。这应该适用于所有平台:
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
"mailto","abc@gmail.com", null));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
emailIntent.putExtra(Intent.EXTRA_TEXT, "Body");
startActivity(Intent.createChooser(emailIntent, "Send email..."));
更新:根据marcwjj,似乎在 4.3 上,我们需要传递字符串数组而不是电子邮件地址的字符串才能使其工作。我们可能需要再添加一行:
intent.putExtra(Intent.EXTRA_EMAIL, addresses); // String[] addresses
主要有以下三种方法:
String email = /* Your email address here */
String subject = /* Your subject here */
String body = /* Your body here */
String chooserTitle = /* Your chooser title here */
<强> 1。自定义 Uri
:
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。使用 Intent
附加功能:
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"));
<强> 3。支持库ShareCompat
:
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();
%
符号,则生成的电子邮件中的某些字符将无法正确编码。您需要执行@minipif 建议的 Uri.encode
舞蹈。
.setType("message/rfc822")
的第三个选项在选择器对话框(Android 8.1)中给了我太多不相关的选项。旧的 Intent.ACTION_SENDTO
方法效果最好!
当您像下面一样更改您的 intent.setType 时,您将得到
intent.setType("text/plain");
使用 android.content.Intent.ACTION_SENDTO
仅获取电子邮件客户端列表,而不获取 facebook 或其他应用程序。只是电子邮件客户端。前任:
new Intent(Intent.ACTION_SENDTO);
我不建议您直接访问电子邮件应用程序。让用户选择他最喜欢的电子邮件应用程序。不要约束他。
如果您使用 ACTION_SENDTO,putExtra 无法向意图添加主题和文本。使用 Uri 添加主题和正文。
编辑:我们可以使用 message/rfc822
而不是 "text/plain"
作为 MIME 类型。然而,这并不表示“只提供电子邮件客户端”——它表示“提供任何支持消息/rfc822 数据的东西”。这可能很容易包括一些不是电子邮件客户端的应用程序。
message/rfc822
支持 .mhtml, .mht, .mime
的 MIME 类型
ACTION_SENDTO
时,意图选择器会显示 no apps installed to perform this intent
。我使用的是 Android 4.1.2,并且我安装了一个电子邮件应用程序......
message/rfc822
类型文件的应用程序列表。这与发送电子邮件不相同。请改用 mailto:
协议,因为这是电子邮件客户端真正理解的内容。
这是从 Android 官方文档中引用的,我已经在 Android 4.4 上对其进行了测试,并且运行良好。在 https://developer.android.com/guide/components/intents-common.html#Email 上查看更多示例
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);
}
}
EXTRA_EMAIL
不适合我,所以我使用了 Uri.parse("mailto: " + myEmail)
一个迟到的答案,虽然我想出了一个可以帮助其他人的解决方案:
爪哇版
Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
emailIntent.setData(Uri.parse("mailto:abc@xyz.com"));
startActivity(Intent.createChooser(emailIntent, "Send feedback"));
Kotlin 版本
val emailIntent = Intent(Intent.ACTION_SENDTO).apply {
data = Uri.parse("mailto:abc@xyz.com")
}
startActivity(Intent.createChooser(emailIntent, "Send feedback"))
这是我的输出(仅建议使用 Gmail + Inbox):
https://i.stack.imgur.com/js4Kw.png
我从 Android Developers 网站获得了这个解决方案。
mailto
和电子邮件之间没有空格:mailto:abc@xyz.com
这对我有用:
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:"));
intent.putExtra(Intent.EXTRA_EMAIL , new String[] { "me@somewhere.com" });
intent.putExtra(Intent.EXTRA_SUBJECT, "My subject");
startActivity(Intent.createChooser(intent, "Email via..."));
即使用ACTION_SENDTO
动作而不是ACTION_SEND
动作。我已经在几个 Android 4.4 设备上尝试过,它限制选择器弹出窗口只显示电子邮件应用程序(电子邮件、Gmail、雅虎邮件等),并且它正确地将电子邮件地址和主题插入到电子邮件中。
尝试:
intent.setType("message/rfc822");
message/rfc822
类型文件的应用程序列表。这与发送电子邮件不相同。请改用 mailto:
协议,因为这是电子邮件客户端真正理解的内容。
这是根据 Android Developer Official Documentation 发送电子邮件意图的正确方法
将这些代码行添加到您的应用程序中:
Intent intent = new Intent(Intent.ACTION_SEND);//common intent
intent.setData(Uri.parse("mailto:")); // only email apps should handle this
可选:添加正文和主题,像这样
intent.putExtra(Intent.EXTRA_SUBJECT, "Your Subject Here");
intent.putExtra(Intent.EXTRA_TEXT, "E-mail body" );
您已经在问题中添加了这一行
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"hello@example.com"});
这将是收件人的地址,这意味着用户将向您(开发人员)发送一封电子邮件。
Intent.ACTION_SEND
更改为 Intent.ACTION_SENDTO
之前,这不起作用。
终于想出最好的办法
String to = "test@gmail.com";
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);
如果您只需要电子邮件客户端,则应将 android.content.Intent.EXTRA_EMAIL
与数组一起使用。这是一个例子:
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);
ACTION_SENDTO
适用于所有安卓版本:
String[] to = {"email@server.com"};
Uri uri = Uri.parse("mailto:email@server.com")
.buildUpon()
.appendQueryParameter("subject", "subject")
.appendQueryParameter("body", "body")
.build();
Intent emailIntent = new Intent(ACTION_SENDTO, uri);
emailIntent.putExtra(EXTRA_EMAIL, TO);
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
为 Android 10 更新,现在使用 Kotlin...
fun Context.sendEmail(
address: String?,
subject: String?,
body: String?,
) {
val recipients = arrayOf(address)
val uri = address.toUri()
.buildUpon()
.appendQueryParameter("subject", subject)
.appendQueryParameter("body", body)
.build()
val emailIntent = Intent(ACTION_SENDTO, uri).apply {
setData("mailto:$address".toUri());
putExtra(EXTRA_SUBJECT, subject);
putExtra(EXTRA_TEXT, body);
putExtra(EXTRA_EMAIL, recipients)
}
val pickerTitle = getString(R.string.some_title)
ContextCompat.startActivity(this, Intent.createChooser(emailIntent, pickerTitle, null)
}
...更新到 API 30 后,代码没有填充电子邮件客户端(例如 Gmail)的主题和正文。但我找到了答案here:
fun Context.sendEmail(
address: String?,
subject: String?,
body: String?,
) {
val selectorIntent = Intent(ACTION_SENDTO)
.setData("mailto:$address".toUri())
val emailIntent = Intent(ACTION_SEND).apply {
putExtra(EXTRA_EMAIL, arrayOf(address))
putExtra(EXTRA_SUBJECT, subject)
putExtra(EXTRA_TEXT, body)
selector = selectorIntent
}
startActivity(Intent.createChooser(emailIntent, getString(R.string.send_email)))
}
以下代码对我来说很好。
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 字符:
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);
}
Uri.encode
是正确的方法。但是为什么要为主体和主体调用两次呢?
这些解决方案都不适合我。这是适用于 Lollipop 的最小解决方案。在我的设备上,只有 Gmail 和本机电子邮件应用程序出现在生成的选择器列表中。
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..."));
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);
}
}
这些答案中的大多数仅适用于您不发送附件的简单情况。就我而言,有时我需要发送附件(ACTION_SEND)或两个附件(ACTION_SEND_MULTIPLE)。
所以我从这个线程中采用了最好的方法并将它们结合起来。它正在使用支持库的 ShareCompat.IntentBuilder
,但我只显示与 ACTION_SENDTO 与“mailto:”uri 匹配的应用程序。通过这种方式,我只获得了带有附件支持的电子邮件应用程序列表:
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
}
如果有人在看,在 Kotlin
val emailArrray:Array<String> = arrayOf("travelagentsupport@kkk.com")
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);
}
以下代码对我有用!
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);
这对我来说非常好:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("mailto:" + address));
startActivity(Intent.createChooser(intent, "E-mail"));
如果您想确保您的意图仅由电子邮件应用程序(而不是其他短信或社交应用程序)处理,请使用 ACTION_SENDTO
操作并包含“mailto:”数据方案。例如:
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#Email 中找到了这个
使用 intent.setType("message/rfc822");
确实有效,但它显示了不一定处理电子邮件的额外应用程序(例如 GDrive)。将 Intent.ACTION_SENDTO
与 setType("text/plain")
一起使用是最好的,但您必须添加 setData(Uri.parse("mailto:"))
才能获得最佳结果(仅限电子邮件应用程序)。完整代码如下:
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setType("text/plain");
intent.setData(Uri.parse("mailto:IT@RMAsoft.NET"));
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"));
如果您想定位 Gmail,那么您可以执行以下操作。请注意,意图是“ACTION_SENDTO”而不是“ACTION_SEND”,Gmail 不需要额外的意图字段。
String uriText =
"mailto:youremail@gmail.com" +
"?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"));
}
我正在更新 Adil 在 Kotlin 中的回答,
val intent = Intent(Intent.ACTION_SENDTO)
intent.data = Uri.parse("mailto:") // only email apps should handle this
intent.putExtra(Intent.EXTRA_EMAIL, Array(1) { "test@email.com" })
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)
}
String sendEmailTo = "abc@xyz.com";
String subject = "Subject";
String body = "Body";
Uri uri = Uri.parse("mailto:"+sendEmailTo+"?subject="+subject+"&body="+body);
startActivity(new Intent(Intent.ACTION_VIEW, uri);
这对我有用。这只会在意图选择器中显示邮件应用程序。
另外:我遇到这种方法的一个问题是我无法在建议和正文中添加空格。因此,要在建议或正文中添加空格,然后将空格替换为 %20
请使用以下代码:
try {
String uriText =
"mailto:emailid" +
"?subject=" + Uri.encode("Feedback for app") +
"&body=" + Uri.encode(deviceInfo);
Uri uri = Uri.parse(uriText);
Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
emailIntent.setData(uri);
startActivity(Intent.createChooser(emailIntent, "Send email using..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(ContactUsActivity.this, "No email clients installed.", Toast.LENGTH_SHORT).show();
}
也许你应该试试这个:intent.setType("plain/text");
我找到了它here。我在我的应用程序中使用过它,它只显示电子邮件和 Gmail 选项。
plain/text
类型文件的应用程序列表,而这甚至不是有效的 MIME 类型。这与发送电子邮件不相同。请改用 mailto:
协议,因为这是电子邮件客户端真正理解的内容。
在电话电子邮件客户端中撰写电子邮件:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("plain/text");
intent.putExtra(Intent.EXTRA_EMAIL, new String[] { "some@email.address" });
intent.putExtra(Intent.EXTRA_SUBJECT, "subject");
intent.putExtra(Intent.EXTRA_TEXT, "mail body");
startActivity(Intent.createChooser(intent, ""));
用这个:
boolean success = EmailIntentBuilder.from(activity)
.to("support@example.org")
.cc("developer@example.org")
.subject("Error report")
.body(buildErrorReport())
.start();
使用构建等级:
compile 'de.cketti.mailto:email-intent-builder:1.0.0'
这是我使用的,它对我有用:
//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);
这也将让用户选择他们喜欢的电子邮件应用程序。唯一不允许您做的是设置收件人的电子邮件地址。
"
。
此代码在我的设备中运行
Intent mIntent = new Intent(Intent.ACTION_SENDTO);
mIntent.setData(Uri.parse("mailto:"));
mIntent.putExtra(Intent.EXTRA_EMAIL , new String[] {"mahendrarajdhami@gmail.com"});
mIntent.putExtra(Intent.EXTRA_SUBJECT, "");
startActivity(Intent.createChooser(mIntent, "Send Email Using..."));
不定期副业成功案例分享
Uri.fromParts("mailto", "", null)