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

上面的代码打开一个对话框,显示以下应用程序:-蓝牙,谷歌文档,雅虎邮件,Gmail, Orkut, Skype等。

实际上,我想过滤这些列表选项。我只想显示电子邮件相关的应用程序,如Gmail和雅虎邮件。怎么做呢?

我曾在Android Market应用中看到过这样的例子。

打开Android Market应用程序 打开任何开发者指定了他/她的电子邮件地址的应用程序。(如果你找不到这样的应用程序,请打开我的应用程序:- market://details?id=com.becomputer06.vehicle.diary.free,或通过“车辆日记”搜索) 向下滚动到“开发者” 点击“发送电子邮件”

对话框只显示电子邮件应用程序,如Gmail,雅虎邮件等。它不显示蓝牙,Orkut等。什么样的代码产生这样的对话?


也许你应该试试这个:intent.setType("plain/text");

我在这里找到的。我在我的应用程序中使用过它,它只显示电子邮件和Gmail选项。


什么时候你会改变你的意图。你将得到如下所示的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类型。然而,这并不是说“只提供电子邮件客户端”——而是说“提供任何支持message/rfc822数据的东西”。这很可能包括一些不是电子邮件客户端的应用程序。

message/rfc822支持.mhtml, .mht, .mime的MIME类型


试一试:

intent.setType("message/rfc822");

更新

官方的方法:

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

编辑:不再工作与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);
}

下面的代码对我来说很好。

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

如果你只想要电子邮件客户端,你应该使用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);

这对我来说很管用:

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,雅虎邮件等);而且 它正确地将电子邮件地址和主题插入到电子邮件中。


这些解决方案对我都不起作用。这里有一个对棒棒糖有效的最小解决方案。在我的设备上,只有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..."));

这是引用自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);
    }
}

在电话邮件客户端撰写邮件:

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, ""));

根据Android开发者官方文档,这是发送电子邮件意图的正确方式

添加以下代码行到你的应用程序:

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

这将是收件人的地址,这意味着用户将向您(开发人员)发送电子邮件。


下面的代码对我有用!!

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

这是一个迟来的答案,尽管我想出了一个可以帮助其他人的解决方案:

Java版本

Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
emailIntent.setData(Uri.parse("mailto:abc@xyz.com"));
startActivity(Intent.createChooser(emailIntent, "Send feedback"));

芬兰湾的科特林版本

val emailIntent = Intent(Intent.ACTION_SENDTO).apply { 
    data = Uri.parse("mailto:abc@xyz.com")
}
startActivity(Intent.createChooser(emailIntent, "Send feedback"))

这是我的输出(只有Gmail +收件箱建议):

我从Android开发者网站上得到了这个解决方案。


适用于所有android版本:

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)。但我在这里找到了答案:

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

}

用这个:

boolean success = EmailIntentBuilder.from(activity)
        .to("support@example.org")
        .cc("developer@example.org")
        .subject("Error report")
        .body(buildErrorReport())
        .start();

使用build gradle:

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

这也可以让用户选择他们喜欢的电子邮件应用程序。唯一不允许你做的是设置收件人的电子邮件地址。


如果你想确保你的意图只被电子邮件应用程序处理(而不是其他短信或社交应用程序),那么使用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 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..."));

终于想出了最好的办法

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

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

这些答案大多只适用于不发送附件的简单情况。在我的情况下,我有时需要发送附件(ACTION_SEND)或两个附件(ACTION_SEND_MULTIPLE)。

所以我从这条线索中选择了最好的方法并将它们结合起来。它使用支持库的ShareCompat。但是我只展示了匹配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
}

如果您想要以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")); 
}

使用intent.setType(”消息/根据rfc822”以);工作,但它显示额外的应用程序,不一定处理电子邮件(如GDrive)。使用意图。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"));

我正在用Kotlin更新Adil的答案,

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

用Anko - kotlin

context.email(email, subject, body)

来自Android开发者文档:

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

只发送到电子邮件客户端-与多个附件

有很多解决方案,但都是局部有效的。

Mailto可以正确地过滤电子邮件应用程序,但它不能不发送流/文件。

Message /rfc822打开了大量的应用程序和电子邮件客户端

所以,这个问题的解决方法是两者都用。

首先使用mailto意图解析意图活动 然后将数据设置为每个已解析的活动以发送所需的数据

private void share()
{
     Intent queryIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:"));
     Intent dataIntent  = getDataIntent();

     Intent targetIntent = getSelectiveIntentChooser(context, queryIntent, dataIntent);
     startActivityForResult(targetIntent);
}

构建所需的数据意图,其中填充需要共享的数据

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

用于根据查询意图过滤所需意图的实用程序类

// 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<android.content.Intent> targetedIntents = new ArrayList<android.content.Intent>();

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

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

请使用以下代码:

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

与Kotlin,工作与Gmail:

val i = Intent(Intent.ACTION_SENDTO).apply {
    type = "text/html"
    data = Uri.parse("mailto:")
    putExtra(Intent.EXTRA_EMAIL, arrayOf(email))
    putExtra(Intent.EXTRA_SUBJECT, subject)
    putExtra(Intent.EXTRA_TITLE, subject)
}
if (packageManager != null && i.resolveActivity(packageManager) != null) {
    startActivity(i)
}

如果有人找到一个解决方案来显示消息…


有时你需要打开默认的电子邮件应用程序来查看收件箱,而不创建新的信件,在这种情况下,它将有帮助:

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_APP_EMAIL);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (intent.resolveActivity(getPackageManager()) != null) {
    startActivity(intent);
}

ACTION_VIEW也有一个简单的解决方案:

  Intent intent = new Intent(Intent.ACTION_VIEW);
        Uri data = Uri.parse("mailto:customer@something.com?subject=Feedback");
        intent.setData(data);
        startActivity(intent);

我几乎在android 11上使用了所有的答案,但它们不能正常工作。其中一些没有将mailto放在所需的字段上,另一些甚至根本不起作用。所以我确实阅读了新的文档,发现mailto电子邮件应该在一个数组中,所以对我有用的最后是在这里。 不管怎样,谢谢你的回答,毕竟他们帮了大忙。

//mail me
findViewById<TextView>(R.id.mailme).setOnTouchListener { _, _ ->
    try {
        val mail: Array<String> = arrayOf("somemail@cc.com")
        val mailme = Intent(Intent.ACTION_SENDTO).apply {
            data = Uri.parse("mailto:")
            putExtra(Intent.EXTRA_EMAIL, mail)
            putExtra(Intent.EXTRA_TEXT, "Hey We Need Your Help With This Issue.")
            putExtra(Intent.EXTRA_SUBJECT, "At Logs Calculator, We Need Your Help !")
        }
        startActivity(mailme)
    } catch (e: Exception) {
        e.printStackTrace()
    }
    true
}

严格按照这个步骤来做,你应该没有问题

https://developer.android.com/guide/components/intents-common#ComposeEmail

确保在使用ACTION_SENDTO时在EXTRA_EMAIL字段中传递一个数组


科特林:

val email: String = getEmail()
val intent = Intent(Intent.ACTION_SENDTO)
intent.data = Uri.parse("mailto:$email" )
startActivity(intent)

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