【问题标题】:Send email via gmail通过 gmail 发送电子邮件
【发布时间】:2012-01-07 06:06:48
【问题描述】:

我有一个用于发送电子邮件的代码

Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_EMAIL,
                new String[] { to });
i.putExtra(Intent.EXTRA_SUBJECT, subject);
i.putExtra(Intent.EXTRA_TEXT, msg);
try {
    startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
    Toast.makeText(Start.this,
                    "There are no email clients installed.",
                    Toast.LENGTH_SHORT).show();
}

但是当这个意图被触发时,我会在列表中看到许多项目,例如 sms 应用、gmail 应用、facebook 应用等等。

我怎样才能过滤这个并只启用 gmail 应用程序(或者可能只是电子邮件应用程序)?

【问题讨论】:

    标签: android email android-intent gmail


    【解决方案1】:
    Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
                        "mailto","opinions@gmail.com.com", null));
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, "IndiaTV News - Mobile App Feedback");
    emailIntent.putExtra(Intent.EXTRA_TEXT,Html.fromHtml(Settings.this.getString(R.string.MailContent)));
    startActivityForResult(Intent.createChooser(emailIntent, "Send email..."),0);
    

    【讨论】:

      【解决方案2】:

      这是从 Android 官方文档中引用的,我已经在 Android 4.4 上对其进行了测试,并且运行良好。在https://developer.android.com/guide/components/intents-common.html#Email查看更多示例

        public void composeEmail(String[] addresses, String subject) {
              Intent intent = new Intent(Intent.ACTION_SENDTO);
              intent.setData(Uri.parse("mailto:")); // only email apps should handle this
              intent.putExtra(Intent.EXTRA_EMAIL, addresses);
              intent.putExtra(Intent.EXTRA_SUBJECT, subject);
              if (intent.resolveActivity(getPackageManager()) != null) {
                  startActivity(intent);
              }
          }
      

      【讨论】:

      • 这对附件的想法没有帮助
      【解决方案3】:

      Igor Popov 的回答是 100% 正确的,但如果您想要一个备用选项,我使用这种方法:

      public static Intent createEmailIntent(final String toEmail, 
                                             final String subject, 
                                             final String message)
      {
          Intent sendTo = new Intent(Intent.ACTION_SENDTO);
          String uriText = "mailto:" + Uri.encode(toEmail) +
                  "?subject=" + Uri.encode(subject) +
                  "&body=" + Uri.encode(message);
          Uri uri = Uri.parse(uriText);
          sendTo.setData(uri);
      
          List<ResolveInfo> resolveInfos = 
              getPackageManager().queryIntentActivities(sendTo, 0);
      
          // Emulators may not like this check...
          if (!resolveInfos.isEmpty())
          {
              return sendTo;
          }
      
          // Nothing resolves send to, so fallback to send...
          Intent send = new Intent(Intent.ACTION_SEND);
      
          send.setType("text/plain");
          send.putExtra(Intent.EXTRA_EMAIL,
                     new String[] { toEmail });
          send.putExtra(Intent.EXTRA_SUBJECT, subject);
          send.putExtra(Intent.EXTRA_TEXT, message);
      
          return Intent.createChooser(send, "Your Title Here");
      }
      

      【讨论】:

      • 将尝试在我的应用程序中使用它。但最后一行 - 选择器 -> createChooser
      • 我会更新我的答案,没有方法chooser,它是createChooser()
      • 如果我想添加附件怎么办?
      • 有两种解决方案,如果你只有一个,你可以使用 Intent.EXTRA_STREAM ,如你所见(虽然我不知道不同的邮件应用程序对此的支持是什么):@987654321 @。仅当您有一个附件时,这才有效。如果您有多个,我认为您必须对附件进行 base64 处理并将其放在正文中,然后从内容链接到它。
      【解决方案4】:

      接受的答案不适用于 4.1.2。这应该适用于所有平台:

      Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
                  "mailto","abc@gmail.com", null));
      emailIntent.putExtra(Intent.EXTRA_SUBJECT, "EXTRA_SUBJECT");
      startActivity(Intent.createChooser(emailIntent, "Send email..."));
      

      希望这会有所帮助。

      【讨论】:

      • 如果不知道收件人的地址,则不能用fromParts构建Uri,但必须使用Uri.parse("mailto:")并将其传递给Intent.setData。然后,如果您在主题之外还有一些正文,您可以使用Intent.EXTRA_TEXT 来传递意图。
      • 如果您不知道收件人的地址,只需在此处输入一个空字符串即可。我刚刚检查过,它工作正常。
      • 如果你这样做,PayPal 应用程序将出现在选择器对话框中,这似乎我只是想偷我的客户一些钱而不是检索反馈:S 请注意,这只发生在上次更新PayPal 应用程序(大约一周前)我们可以防止这种情况发生吗?
      【解决方案5】:

      使用android.content.Intent.ACTION_SENDTO (new Intent(Intent.ACTION_SENDTO);) 仅获取电子邮件客户端列表,不使用 facebook 或其他应用程序。只是电子邮件客户端。

      我不建议您直接使用电子邮件应用程序。让用户选择他最喜欢的电子邮件应用程序。 不要约束他。

      如果您使用 ACTION_SENDTO,putExtra 无法向意图添加主题和文本。使用 Uri 添加主题和正文。

      示例

      Intent send = new Intent(Intent.ACTION_SENDTO);
      String uriText = "mailto:" + Uri.encode("email@gmail.com") + 
                "?subject=" + Uri.encode("the subject") + 
                "&body=" + Uri.encode("the body of the message");
      Uri uri = Uri.parse(uriText);
      
      send.setData(uri);
      startActivity(Intent.createChooser(send, "Send mail..."));
      

      【讨论】:

      • 您能告诉我如何在其中添加图像吗?我试过 &attach=(imagefilepath)。但是,它似乎不起作用!
      • 当我使用ACTION_SENDTO 时,意图选择器显示no apps installed to perform this intent。我使用的是 Android 4.1.2,并且我安装了一个电子邮件应用程序...
      • 当我从 ACTION_SENT 更改为 ACTION_SENDTO 时,我得到了相同的消息
      • 在 Android 6.0.1 中就像一个魅力。
      【解决方案6】:

      替换

      i.setType("text/plain");
      

      // need this to prompts email client only
      i.setType("message/rfc822");
      

      【讨论】:

      • 使用 MIME 类型执行发送操作是个坏主意,因为您基本上是在指示 Android 提供支持发送 message/rfc822 类型文件的应用列表。这与发送电子邮件相同。请改用mailto: 协议,因为这是电子邮件客户端真正理解的内容。
      • 是的,但是 mailto: 不支持附件。
      • @EdBurnette 其实“mailto:”是否支持附件取决于邮件应用。例如,最新的“K9 Mail”支持使用带有附件的mailto。
      • 多个附件使 Gmail 与 mailto: 一起崩溃,很遗憾。 :(
      • 想出了如何处理多个附件:stackoverflow.com/questions/22240028/…
      猜你喜欢
      • 2023-03-19
      • 2016-09-26
      • 2011-06-01
      • 2018-03-21
      • 2016-09-09
      • 2019-01-24
      相关资源
      最近更新 更多