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等。什么样的代码产生这样的对话?


当前回答

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

其他回答

这段代码在我的设备中工作

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

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

主要有三种方法:

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

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