【问题标题】:ACTION_SEND force sending with emailACTION_SEND 强制使用电子邮件发送
【发布时间】:2012-04-24 08:30:38
【问题描述】:

每次我创建一个从我的应用程序发送电子邮件的操作时,它都会提示许多选项,包括 QR 客户端...

有没有办法强制只通过电子邮件客户端发送?

发送电子邮件的代码

String rec[] = { owner.email };
i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(android.content.Intent.EXTRA_EMAIL, rec);
i.putExtra(android.content.Intent.EXTRA_SUBJECT, "RE: " + desc);
i.putExtra(android.content.Intent.EXTRA_TEXT,
        "\n\n\nSent from Mojo for Android");
startActivity(i);

我启动它时发生的屏幕截图

【问题讨论】:

标签: android email android-intent


【解决方案1】:

尝试设置类型message/rfc822 而不是text/plain

【讨论】:

  • 有没有办法缩小范围以强制它使用 Gmail 发送?
  • 如果我必须使用 setType 作为我的附件(文件)类型? +1 如果我只需要发送文本
  • text/html 呢?
【解决方案2】:
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("text/html");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
new String[] { "abc@xyz.com" });
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
                    "Subject of the Mail");
emailIntent.putExtra( android.content.Intent.EXTRA_TEXT,
                           "This is my sample Mail");
emailIntent.setType("vnd.android.cursor.dir/email");
startActivity(Intent.createChooser(emailIntent, "Email:"));

否则使用它只会显示邮件客户端,

Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("message/rfc822");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
new String[] { "abc@xyz.com" });
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
                    "Subject of the Mail");
emailIntent.putExtra( android.content.Intent.EXTRA_TEXT,
                           "This is my sample Mail");
//emailIntent.setType("vnd.android.cursor.dir/email");
startActivity(Intent.createChooser(emailIntent, "Email:"));

【讨论】:

    【解决方案3】:

    我觉得你应该把setType改成

    i.setType("message/rfc822") ;
    

    【讨论】:

    • 是的,这默认为在我的应用中打开 Gmail。谢谢!
    • 它打开了更多的应用程序,而不仅仅是电子邮件...,为什么不text/html
    【解决方案4】:

    尽管对@thepoosh 来说为时已晚,但它可能对未来的提问者有所帮助。经过大量的搜索和测试,我终于找到了一个好的解决方案。 感谢开源开发者cketti 分享他/她的简洁解决方案。

    String mailto = "mailto:bob@example.org" +
        "?cc=" + "alice@example.com" +
        "&subject=" + Uri.encode(subject) +
        "&body=" + Uri.encode(bodyText);
    
    Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
    emailIntent.setData(Uri.parse(mailto));
    
    try {
      startActivity(emailIntent);
    } catch (ActivityNotFoundException e) {
      //TODO: Handle case where no email app is available
    }
    

    This 是指向他/她的要点的链接。

    【讨论】:

      【解决方案5】:

      它将显示安装在安卓手机上的所有可用应用程序,这些应用程序可以执行共享或从 webview 发送链接给其他人。喜欢 - Gmail、facebook、imo、whatsapp、messenger 等。

      Intent intent = new Intent(Intent.ACTION_SEND);
      intent.setType("text/plain");
      String shareLink = webView.getUrl();
      intent.putExtra(Intent.EXTRA_TEXT, shareLink);
      startActivity(Intent.createChooser(intent, "Share via..."));
      

      但是当你只强制打开邮件应用时:

      Intent intent = new Intent(Intent.ACTION_SENDTO);
      intent.setData(Uri.parse("mailto:"));
      intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"something@gmail.com"});
      
      try {
          startActivity(Intent.createChooser(intent, "send mail"));
      } catch (ActivityNotFoundException ex) {
          Toast.makeText(this, "No mail app found!!!", Toast.LENGTH_SHORT);
      } catch (Exception ex) {
          Toast.makeText(this, "Unexpected Error!!!", Toast.LENGTH_SHORT);
      }
      

      【讨论】:

        【解决方案6】:

        只要您使用带有text/plain 类型的ACTION_SEND,它将显示所有有效选项。但是,如果您愿意,您可以设计您自己的对话窗口,通过以编程方式进行过滤,仅显示 Gmail 或其他邮件客户端。

        顺便说一句,当你只想使用 Gmail 时,为什么还需要这个窗口?

        【讨论】:

        • 如果我没记错的话,您不能强制将 Gmail 作为默认电子邮件客户端
        • 不,我要求您在自定义设计的对话窗口中显示 Gmail
        • 我不想强迫我的用户使用特定的电子邮件客户端。这似乎不是一个好主意。
        【解决方案7】:
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("mailto:?to=email&subject=hello&body=hello%20world"));
        startActivity(Intent.createChooser(intent, "Send via..."));
        

        你可以试试这个:::::

        【讨论】:

          【解决方案8】:
          Intent.setType("plain/text");
          

          一开始当我发现这个时,我立刻以为这是一个错误,应该是text/plain,但这实际上是只在应用程序列表中显示电子邮件客户端的正确方法。

          试一试,自己看看。

          【讨论】:

            【解决方案9】:

            intent.setPackage("com.google.android.gm"); //添加Gmail包强制打开Gmail App

            【讨论】:

              【解决方案10】:
              String rec[] = { owner.email };
              i = new Intent(Intent.ACTION_SEND);
              i.setType("message/rfc822") ;
              i.putExtra(android.content.Intent.EXTRA_EMAIL, rec);
              i.putExtra(android.content.Intent.EXTRA_SUBJECT, "RE: " + desc);
              i.putExtra(android.content.Intent.EXTRA_TEXT,
                      "\n\n\nSent from Mojo for Android");
              startActivity(i);
              

              试试这个;:::

              【讨论】:

                猜你喜欢
                • 2018-08-20
                • 2016-09-20
                • 2011-11-07
                • 1970-01-01
                • 2016-04-17
                • 2017-12-07
                • 1970-01-01
                • 1970-01-01
                • 2014-07-12
                相关资源
                最近更新 更多