当点击按钮时从Android应用程序发送电子邮件

24

我需要为用户提供一个功能,让他们可以通过发送电子邮件来分享一些数据。我使用了以下代码。

    Intent email = new Intent(android.content.Intent.ACTION_SENDTO);
    email.setType("message/rfc822");
    email.putExtra(Intent.EXTRA_EMAIL, new String[] { to });
    email.putExtra(Intent.EXTRA_SUBJECT, subject);
    email.putExtra(Intent.EXTRA_TEXT, message);
    startActivity(Intent.createChooser(email,"Choose an Email client :"));

这显示了用户可以选择电子邮件、Gmail、Skype和通过蓝牙发送。我不想让用户在此列表中看到Skype和通过蓝牙发送的选项。我该怎么做?我的手机上有WhatsApp,它可以完成同样的功能,但在列表中不会显示电子邮件和蓝牙选项(设置->帮助->联系我们->...)。只会显示电子邮件和Gmail选项。我需要做同样的事情。

2个回答

46

试一下:

Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
            "mailto","email@email.com", null));
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_TEXT, message);
startActivity(Intent.createChooser(intent, "Choose an Email client :"));

如果您没有特定的收件人 - 就这样做:

Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
            "mailto", "", null));

我检查了一下。我只想在列表中列出邮件客户端(Gmail、Yahoo、Outlook),而不是Skype或通过蓝牙发送。我该怎么办? - Amardeepvijay
@Amardeep 编辑了我的答案。然而,WhatsApp 仍然使用自定义对话框,在列表中仅显示2个电子邮件客户端(Gmail 和 Email),即使您拥有超过2个电子邮件客户端。 - localhost
它在实际设备上无法工作。我可以使用这个email.setType("message/rfc822")吗? - Amardeepvijay
@Amardeep 有任何错误吗?在我的三星Galaxy Note Android 4.1.2上可以运行。 - localhost

1

使用此方法仅通过Gmail分享,您只需要调用

startActivity(getSendEmailIntent(context, email,subject, body));





public Intent getSendEmailIntent(Context context, String email,
                    String subject, String body) {
            Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);

            try {

                // Explicitly only use Gmail to send
                emailIntent.setClassName("com.google.android.gm",
                        "com.google.android.gm.ComposeActivityGmail");

                emailIntent.setType("text/html");

                // Add the recipients
                if (email != null)
                    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
                            new String[] { email });

                if (subject != null)
                    emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
                            subject);

                if (body != null)
                    emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml(body));

                // Add the attachment by specifying a reference to our custom
                // ContentProvider
                // and the specific file of interest
                // emailIntent.putExtra(
                // Intent.EXTRA_STREAM,
                // Uri.parse("content://" + CachedFileProvider.AUTHORITY + "/"
                // + fileName));

                 return emailIntent;
    //          myContext.startActivity(emailIntent);
            } catch (Exception e) {
                emailIntent.setType("text/html");

                // Add the recipients
                if (email != null)
                    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
                            new String[] { email });

                if (subject != null)
                    emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
                            subject);

                if (body != null)
                    emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml(body));

    //          myContext.startActivity(Intent.createChooser(emailIntent,
    //                  "Share Via"));
                 return emailIntent;
            }
            }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接