【问题标题】:Sending Email from Android app when click on button单击按钮时从 Android 应用程序发送电子邮件
【发布时间】:2014-02-12 06:48:02
【问题描述】:

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

    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。我也需要这样做。

【问题讨论】:

    标签: android email android-intent


    【解决方案1】:

    试试这个:

    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,通过蓝牙发送。我做什么..?
    • @Amardeep 编辑了我的答案。但是,WhatsApp 仍然使用自定义对话框,即使您有超过 2 个电子邮件客户端,列表中也只显示 2 个电子邮件客户端(Gmail 和电子邮件)。
    • 它在实际设备中不起作用。我可以用这个 email.setType("message/rfc822");
    • @Amardeep 有什么错误吗?它适用于三星 Galaxy Note Android 4.1.2。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-07
      • 2011-08-06
      • 1970-01-01
      • 1970-01-01
      • 2011-11-14
      • 2011-08-04
      相关资源
      最近更新 更多