发送电子邮件意图

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”应用程序上看到了这样的例子。

  1. 打开Android Market应用
  2. 打开开发人员指定其电子邮件地址的任何应用程序。 (如果你找不到这样的应用程序只需打开我的应用程序: - market:// details?id = com.becomputer06.vehicle.diary.free,或者通过'Vehicle Diary'搜索)
  3. 向下滚动到'DEVELOPER'
  4. 点击“发送电子邮件”

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


#1楼

接受的答案不适用于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

参考链接


#2楼

主要有三种方法:

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

#3楼

编辑:使用新版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);
}

#4楼

以下代码对我很有用。

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

#5楼

如果您只想要电子邮件客户端,则应将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);

#6楼

这对我有用:

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


#7楼

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

#8楼

这是从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);
    }
}

#9楼

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

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

#10楼

这是根据Android Developer Docs的方式将这些代码行添加到您的应用中:

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

#11楼

以下代码为我工作!!

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

#12楼

这对我很有用:

    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setData(Uri.parse("mailto:" + address));
    startActivity(Intent.createChooser(intent, "E-mail"));

#13楼

一个迟到的答案,虽然我找到了一个可以帮助他人的解决方案:

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

这是我的输出(仅建议使用Gmail +收件箱):

我的输出

我从Android Developers网站获得了这个解决方案。


#14楼

适用于所有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(Intent.ACTION_SENDTO, uri);
    emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
    startActivity(Intent.createChooser(emailIntent, "Send mail..."));

#15楼

用这个:

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'

#16楼

这是我使用的,它适用于我:

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

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


#17楼

如果您想确保仅通过电子邮件应用程序(而不是其他文本消息或社交应用程序)处理您的意图,请使用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中找到了这个


#18楼

此代码在我的设备中运行

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

#19楼

最后拿出最好的办法

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

#20楼

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

#21楼

大多数这些答案仅适用于您未发送附件的简单情况。 在我的情况下,我有时需要发送附件(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
}

#22楼

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

#23楼

使用intent.setType("message/rfc822"); 确实有效,但它显示了不一定处理电子邮件的额外应用程序(例如GDrive)。 使用Intent.ACTION_SENDTOsetType("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"));

#24楼

我正在更新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)
}

#25楼

使用Anko - kotlin

context.email(email, subject, body)

#26楼

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

#27楼

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

#28楼

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

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


#29楼

当你改变你的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类型


#30楼

尝试:

intent.setType("message/rfc822");
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值