【问题标题】:Android: Using Intent to send email - only offering email applications doesnt workAndroid:使用 Intent 发送电子邮件 - 仅提供电子邮件应用程序不起作用
【发布时间】:2017-01-02 15:58:03
【问题描述】:

我正在尝试创建一个发送电子邮件的意图活动。 使用

public void emailSend(View view){
        Intent emailIntent = new Intent(Intent.ACTION_SEND);
        emailIntent.setData(Uri.parse("mailto:"));
        emailIntent.setType("plain/text");
        emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Nächstes Treffen");
        emailIntent.putExtra(Intent.EXTRA_EMAIL,adressListe);
        if (emailIntent.resolveActivity(getPackageManager()) != null){
            startActivity(emailIntent);
        }
    }

为我提供的不仅仅是电子邮件应用程序。 使用

public void emailSend(View view){
        Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
        emailIntent.setData(Uri.parse("mailto:"));
        emailIntent.setType("plain/text");
        emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Nächstes Treffen");
        emailIntent.putExtra(Intent.EXTRA_EMAIL,adressListe);
        if (emailIntent.resolveActivity(getPackageManager()) != null){
            startActivity(emailIntent);
        }
    }

单击按钮时没有任何反应。 对于

emailIntent.setType("plain/text");

也试过了

emailIntent.setType("messageage/rfc822");

emailIntent.setType("*/*");

都有不同的结果,但没有一个只是显示电子邮件应用程序。

你知道如何解决这个问题吗?非常感谢您的帮助!

谢谢!

【问题讨论】:

  • 你试过不使用setType吗?
  • plain/text 不是有效的 MIME 类型。使用第二种方法作为起点,但去掉EXTRA_EMAIL 并将该地址放在Uri 中(就像mailto:... 中的...)。请记住,用户可能无法访问支持此功能的电子邮件程序,因此如果resolveActivity() 返回null,您需要有一个else 块。
  • 您也可以在标题电子邮件下查看developer.android.com/guide/components/intents-common.html
  • @Prera​​kSola 是的,我做到了。
  • @CommonsWare 我将如何添加它? Intent emailIntent = new Intent(Intent.ACTION_SENDTO); emailIntent.setData(Uri.parse("mailto:"+adressListe)); emailIntent.setType("message/rfc822"); 什么都不做。

标签: java android email android-intent


【解决方案1】:

当我使用意图时 android.content.Intent.ACTION_SENDTO 对我不起作用,因为它显示了许多应用程序,有些应用程序不是电子邮件客户端。我找到了这种方式,它对我来说非常有效。

Intent testIntent = new Intent(Intent.ACTION_VIEW);  
Uri data = Uri.parse("mailto:?subject=" + "text subject" + "&body=" + "text content" + "&to=" + "email@example.com");  
testIntent.setData(data);  
startActivity(testIntent);

【讨论】:

    【解决方案2】:

    这是我正在工作的代码:

    Intent intentMail = new Intent(Intent.ACTION_SEND);
    intentMail.setType("message/rfc822");
    intentMail.putExtra(Intent.EXTRA_EMAIL, new String[]{
                        "mailto@email.com" }); // the To mail.
    intentMail.putExtra(Intent.EXTRA_SUBJECT, "Subject goes here");
    intentMail.putExtra(Intent.EXTRA_TEXT, "Content goes here");
    
    // now we have created the mail, lets try and send it.
    try {
        startActivity(Intent.createChooser(intentMail, "Message to User to do what next"));
    } catch (android.content.ActivityNotFoundException ex) {
        Toast.makeText(Activity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
    }
    

    如果这不起作用,请告诉我。哦,你有错误日志吗?

    *edit,这是我找到我的代码的地方,所以重复一下。 Source

    【讨论】:

    • 您好,感谢您的回答。我拥有与您一样的一切,不同之处在于我之前声明的 adressListe String [] adressListe = adressen.split(","); 与 adressen 是一个包含多个地址的字符串。然而,当我点击我的按钮时,它甚至为我提供了 oneDrive....
    【解决方案3】:

    使用下一种方法时,我只看到电子邮件客户端:

        Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
        emailIntent.setData(Uri.parse("mailto:"));
        emailIntent.putExtra(Intent.EXTRA_EMAIL, new String [] {someaddress@gmail.com}); 
        emailIntent.putExtra(Intent.EXTRA_SUBJECT,"Email subject"));
        Intent chooser = Intent.createChooser(emailIntent, "Mail to ..");
        if (emailIntent.resolveActivity(getPackageManager()) != null) {
            startActivity(chooser);
        }
        else
           //Do something if there's no Email client
    

    【讨论】:

      【解决方案4】:

      最后在尝试了所有这些帖子的变体之后,我想出了这个效果很好:

      fun sendEmail(context: Context, email: String, subject: String = "") {
          try {
              context.startActivity(
                  Intent(Intent.ACTION_SENDTO).apply {
                      data = (Uri.parse("mailto:$email"))
                          .buildUpon()
                          .appendQueryParameter(
                              "subject", subject
                          ).appendQueryParameter(
                              "to", email
                          )
                          .build()
                  }
              )
          } catch (e: Exception) {
              Timber.e("failed to open mail client")
          }
      }
      

      【讨论】:

        【解决方案5】:

        这样就可以了!

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

        【讨论】:

        • 认为这是朝着正确的方向发展。至少现在我什至可以直接向我经常联系的联系人之一发送电子邮件。但仍然提供“保存在谷歌驱动器”之类的东西.....
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-09-08
        • 1970-01-01
        • 1970-01-01
        • 2011-08-06
        • 1970-01-01
        相关资源
        最近更新 更多