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

时间:2014-02-12 06:48:03

标签: android email android-intent

我需要为用户提供功能,用户可以通过发送电子邮件来共享某些数据。我使用下面的代码。

    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 个答案:

答案 0 :(得分:34)

试试这个:

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

答案 1 :(得分:0)

使用此方法仅通过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;
            }
            }