【问题标题】:How to send emails from my Android application?如何从我的 Android 应用程序发送电子邮件?
【发布时间】:2011-01-12 22:54:47
【问题描述】:

我正在开发一个 Android 应用程序。我不知道如何从应用程序发送电子邮件?

【问题讨论】:

标签: android email


【解决方案1】:

最好(也是最简单)的方法是使用Intent

Intent i = new Intent(Intent.ACTION_SEND);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"recipient@example.com"});
i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
i.putExtra(Intent.EXTRA_TEXT   , "body of email");
try {
    startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
    Toast.makeText(MyActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}

否则您将不得不编写自己的客户端。

【讨论】:

  • 上面的代码中,没有发件人的email id,那么邮件是怎么发送的呢?
  • KIRAN:您需要研究 Intent 的工作原理才能理解这一点。它基本上会打开一个已填写收件人、主题和正文的电子邮件应用程序。由电子邮件应用程序进行发送。
  • 启动活动后,电子邮件未出现在“收件人”字段中。有人知道吗?
  • 这条评论最大的贡献是:message/rfc822
  • 添加这些以确保选择器仅显示电子邮件应用程序:Intent i = new Intent(Intent.ACTION_SENDTO);i.setType("message/rfc822");i.setData(Uri.parse("mailto:"));
【解决方案2】:

使用.setType("message/rfc822"),否则选择器将向您显示所有(许多)支持发送意图的应用程序。

【讨论】:

  • 很好,这应该有更多的赞成票。您不会注意到模拟器上的测试,但是当您在真实设备上发送“文本/纯文本”时,它会给您一个包含 15 多个应用程序的列表!所以绝对推荐使用“message/rfc822”(电子邮件标准)。
  • @Blundell 嗨,但我改成message/rfc822后没有发现任何区别
  • 你能从列表中删除蓝牙吗?这也显示在这种类型中。 +1 不过,巧妙的技巧!
  • 救了我们的培根。无法想象向客户解释用户可能会发推文而不是通过电子邮件发送支持请求。
  • +1111111 这值得无休止的 +1,以便其他人可以看到。我错过了这部分,不得不处理这个问题一段时间!
【解决方案3】:

我很久以前就一直在使用它,它看起来不错,没有出现非电子邮件应用程序。发送电子邮件意图的另一种方式:

Intent intent = new Intent(Intent.ACTION_SENDTO); // it's not ACTION_SEND
intent.putExtra(Intent.EXTRA_SUBJECT, "Subject of email");
intent.putExtra(Intent.EXTRA_TEXT, "Body of email");
intent.setData(Uri.parse("mailto:default@recipient.com")); // or just "mailto:" for blank
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // this will make such that when user returns to your app, your app is displayed, instead of the email app.
startActivity(intent);

【讨论】:

  • 不支持的操作:当前不支持此操作
  • lgor G->请从 setType"(plain/text") 更改为 setType("text/plain")
  • .setType("message/rfc822") 不是文本/纯文本
  • 此代码将打开电子邮件意图?如何在不向用户@yuku 显示意图的情况下发送电子邮件我想将密码发送到电子邮件
  • 这个答案是quite influential。 :)
【解决方案4】:

我正在使用与当前接受的答案类似的东西来发送带有附加二进制错误日志文件的电子邮件。 GMail 和 K-9 可以很好地发送它,它也可以很好地到达我的邮件服务器。唯一的问题是我选择的邮件客户端 Thunderbird 无法打开/保存附加的日志文件。事实上,它根本没有在没有抱怨的情况下保存文件。

我查看了其中一封邮件的源代码,发现日志文件附件具有(可以理解的)mime 类型message/rfc822。当然,该附件不是附加的电子邮件。但是 Thunderbird 无法优雅地处理这个微小的错误。所以这有点令人失望。

经过一番研究和实验,我想出了以下解决方案:

public Intent createEmailOnlyChooserIntent(Intent source,
    CharSequence chooserTitle) {
    Stack<Intent> intents = new Stack<Intent>();
    Intent i = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto",
            "info@domain.com", null));
    List<ResolveInfo> activities = getPackageManager()
            .queryIntentActivities(i, 0);

    for(ResolveInfo ri : activities) {
        Intent target = new Intent(source);
        target.setPackage(ri.activityInfo.packageName);
        intents.add(target);
    }

    if(!intents.isEmpty()) {
        Intent chooserIntent = Intent.createChooser(intents.remove(0),
                chooserTitle);
        chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
                intents.toArray(new Parcelable[intents.size()]));

        return chooserIntent;
    } else {
        return Intent.createChooser(source, chooserTitle);
    }
}

可以这样使用:

Intent i = new Intent(Intent.ACTION_SEND);
i.setType("*/*");
i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(crashLogFile));
i.putExtra(Intent.EXTRA_EMAIL, new String[] {
    ANDROID_SUPPORT_EMAIL
});
i.putExtra(Intent.EXTRA_SUBJECT, "Crash report");
i.putExtra(Intent.EXTRA_TEXT, "Some crash report details");

startActivity(createEmailOnlyChooserIntent(i, "Send via email"));

如您所见,createEmailOnlyChooserIntent 方法可以轻松地输入正确的意图和正确的 mime 类型。

然后,它会遍历响应 ACTION_SENDTO mailto 协议意图(仅限电子邮件应用程序)的可用活动列表,并根据该活动列表和具有正确 mime 类型的原始 ACTION_SEND 意图构造一个选择器。

另一个优点是不再列出 Skype(恰好响应 rfc822 mime 类型)。

【讨论】:

  • 我刚刚为您插入了代码 sn-p,它工作正常。之前已经列出了 Google Drive、Skype 等应用程序。但是没有办法在不调用另一个应用程序的情况下从应用程序发送邮件吗?我刚刚阅读了关于@Rene postet 上面的电子邮件客户端的文章,但似乎太复杂了,无法发送简单的电子邮件
  • 优秀的答案。我还让 Skype 和 Google Drive 提供了ACTION_SEND,这很好地解决了问题。
  • 上面最流行的解决方案也返回了 Skype 和 Vkontakte。这个解决方案更好。
  • 什么是 crashLogFile ?它在哪里初始化?pease sepecify
  • @Noufal 这只是我自己的代码库中的一些剩余部分。这是一个 File 实例,指向我的 Android 应用程序在后台创建的崩溃日志文件,以防出现未捕获的异常。该示例应仅说明如何添加电子邮件附件。您还可以附加外部存储中的任何其他文件(例如图像)。您也可以使用 crashLogFile 删除该行以获得一个工作示例。
【解决方案5】:

让电子邮件应用程序解决您的意图,您需要将 ACTION_SENDTO 指定为操作,将 mailto 指定为数据。

private void sendEmail(){

    Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
    emailIntent.setData(Uri.parse("mailto:" + "recipient@example.com")); // You can use "mailto:" if you don't know the address beforehand.
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, "My email's subject");
    emailIntent.putExtra(Intent.EXTRA_TEXT, "My email's body");
    
    try {
        startActivity(Intent.createChooser(emailIntent, "Send email using..."));
    } catch (android.content.ActivityNotFoundException ex) {
        Toast.makeText(Activity.this, "No email clients installed.", Toast.LENGTH_SHORT).show();
    }

}

【讨论】:

    【解决方案6】:

    解决方案很简单:android 文档对此进行了解释。

    (https://developer.android.com/guide/components/intents-common.html#Email)

    最重要的是标志:它是ACTION_SENDTO,而不是ACTION_SEND

    另一条重要的线是

    intent.setData(Uri.parse("mailto:")); ***// only email apps should handle this***
    

    顺便说一句,如果你发送一个空的Extra,最后的if() 将不起作用,应用程序也不会启动电子邮件客户端。

    根据 Android 文档。如果您想确保您的意图仅由电子邮件应用程序(而不是其他短信或社交应用程序)处理,请使用 ACTION_SENDTO 操作并包含“mailto: ”数据方案。例如:

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

    【讨论】:

      【解决方案7】:

      使用.setType("message/rfc822")ACTION_SEND 的策略似乎也适合非电子邮件客户端的应用,例如Android Beam蓝牙

      使用ACTION_SENDTOmailto: URI 似乎可以完美运行,而is recommended in the developer documentation。但是,如果您在官方模拟器上执行此操作并且没有设置任何电子邮件帐户(或没有任何邮件客户端),则会收到以下错误:

      不支持的操作

      目前不支持该操作。

      如下图:

      事实证明,模拟器将意图解析为名为@9​​87654322@ 的活动,该活动显示上述消息。 Apparently this is by design.

      如果您希望您的应用规避此问题,使其在官方模拟器上也能正常运行,您可以在尝试发送电子邮件之前检查它:

      private void sendEmail() {
          Intent intent = new Intent(Intent.ACTION_SENDTO)
              .setData(new Uri.Builder().scheme("mailto").build())
              .putExtra(Intent.EXTRA_EMAIL, new String[]{ "John Smith <johnsmith@yourdomain.com>" })
              .putExtra(Intent.EXTRA_SUBJECT, "Email subject")
              .putExtra(Intent.EXTRA_TEXT, "Email body")
          ;
      
          ComponentName emailApp = intent.resolveActivity(getPackageManager());
          ComponentName unsupportedAction = ComponentName.unflattenFromString("com.android.fallback/.Fallback");
          if (emailApp != null && !emailApp.equals(unsupportedAction))
              try {
                  // Needed to customise the chooser dialog title since it might default to "Share with"
                  // Note that the chooser will still be skipped if only one app is matched
                  Intent chooser = Intent.createChooser(intent, "Send email with");
                  startActivity(chooser);
                  return;
              }
              catch (ActivityNotFoundException ignored) {
              }
      
          Toast
              .makeText(this, "Couldn't find an email app and account", Toast.LENGTH_LONG)
              .show();
      }
      

      the developer documentation 中查找更多信息。

      【讨论】:

        【解决方案8】:

        可以使用不需要配置的 Intent 发送电子邮件。但是这需要用户交互,并且布局会受到一些限制。

        在没有用户交互的情况下构建和发送更复杂的电子邮件需要构建您自己的客户端。首先是用于电子邮件的 Sun Java API 不可用。我已经成功地利用 Apache Mime4j 库来构建电子邮件。全部基于nilvec 的文档。

        【讨论】:

          【解决方案9】:

          这是在安卓设备中打开邮件应用程序并在撰写邮件中自动填充收件人地址主题的示例工作代码.

          protected void sendEmail() {
              Intent intent = new Intent(Intent.ACTION_SENDTO);
              intent.setData(Uri.parse("mailto:feedback@gmail.com"));
              intent.putExtra(Intent.EXTRA_SUBJECT, "Feedback");
              if (intent.resolveActivity(getPackageManager()) != null) {
                  startActivity(intent);
              }
          }
          

          【讨论】:

          • 谢谢。与@Avi Parshan 的解决方案相比,您将电子邮件设置为setData(),而Avi 设置为putExtra()。两种变体都有效。但如果删除setData 并仅使用intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"hi@example.com"});,就会有ActivityNotFoundException
          【解决方案10】:

          我在我的应用程序中使用以下代码。这准确显示了电子邮件客户端应用程序,例如 Gmail。

              Intent contactIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto", getString(R.string.email_to), null));
              contactIntent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.email_subject));
              startActivity(Intent.createChooser(contactIntent, getString(R.string.email_chooser)));
          

          【讨论】:

            【解决方案11】:

            这将只显示电子邮件客户端(以及出于某种未知原因的 PayPal)

             public void composeEmail() {
            
                Intent intent = new Intent(Intent.ACTION_SENDTO);
                intent.setData(Uri.parse("mailto:"));
                intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"hi@example.com"});
                intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
                intent.putExtra(Intent.EXTRA_TEXT, "Body");
                try {
                    startActivity(Intent.createChooser(intent, "Send mail..."));
                } catch (android.content.ActivityNotFoundException ex) {
                    Toast.makeText(MainActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
                }
            }
            

            【讨论】:

            • 不错的解决方案!它避免了许多不合适的应用程序(主要用作“共享”)。不要在此处添加intent.type = "message/rfc822"; intent.type = "text/html";,否则会导致异常。
            【解决方案12】:

            我就是这样做的。漂亮又简单。

            String emailUrl = "mailto:email@example.com?subject=Subject Text&body=Body Text";
                    Intent request = new Intent(Intent.ACTION_VIEW);
                    request.setData(Uri.parse(emailUrl));
                    startActivity(request);
            

            【讨论】:

              【解决方案13】:

              此功能首先直接intent gmail发送电子邮件,如果找不到gmail,则提升intent selector。我在许多商业应用程序中使用了这个功能,它工作正常。希望对你有帮助:

              public static void sentEmail(Context mContext, String[] addresses, String subject, String body) {
              
                  try {
                      Intent sendIntentGmail = new Intent(Intent.ACTION_VIEW);
                      sendIntentGmail.setType("plain/text");
                      sendIntentGmail.setData(Uri.parse(TextUtils.join(",", addresses)));
                      sendIntentGmail.setClassName("com.google.android.gm", "com.google.android.gm.ComposeActivityGmail");
                      sendIntentGmail.putExtra(Intent.EXTRA_EMAIL, addresses);
                      if (subject != null) sendIntentGmail.putExtra(Intent.EXTRA_SUBJECT, subject);
                      if (body != null) sendIntentGmail.putExtra(Intent.EXTRA_TEXT, body);
                      mContext.startActivity(sendIntentGmail);
                  } catch (Exception e) {
                      //When Gmail App is not installed or disable
                      Intent sendIntentIfGmailFail = new Intent(Intent.ACTION_SEND);
                      sendIntentIfGmailFail.setType("*/*");
                      sendIntentIfGmailFail.putExtra(Intent.EXTRA_EMAIL, addresses);
                      if (subject != null) sendIntentIfGmailFail.putExtra(Intent.EXTRA_SUBJECT, subject);
                      if (body != null) sendIntentIfGmailFail.putExtra(Intent.EXTRA_TEXT, body);
                      if (sendIntentIfGmailFail.resolveActivity(mContext.getPackageManager()) != null) {
                          mContext.startActivity(sendIntentIfGmailFail);
                      }
                  }
              }
              

              【讨论】:

              • 非常感谢。拯救我的生命
              【解决方案14】:

              我使用此代码通过直接启动默认邮件应用撰写部分来发送邮件。

                  Intent i = new Intent(Intent.ACTION_SENDTO);
                  i.setType("message/rfc822"); 
                  i.setData(Uri.parse("mailto:"));
                  i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"test@gmail.com"});
                  i.putExtra(Intent.EXTRA_SUBJECT, "Subject");
                  i.putExtra(Intent.EXTRA_TEXT   , "body of email");
                  try {
                      startActivity(Intent.createChooser(i, "Send mail..."));
                  } catch (android.content.ActivityNotFoundException ex) {
                      Toast.makeText(this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
                  }
              

              【讨论】:

                【解决方案15】:

                简单试试这个

                 public void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.main);
                
                    buttonSend = (Button) findViewById(R.id.buttonSend);
                    textTo = (EditText) findViewById(R.id.editTextTo);
                    textSubject = (EditText) findViewById(R.id.editTextSubject);
                    textMessage = (EditText) findViewById(R.id.editTextMessage);
                
                    buttonSend.setOnClickListener(new OnClickListener() {
                
                        @Override
                        public void onClick(View v) {
                
                            String to = textTo.getText().toString();
                            String subject = textSubject.getText().toString();
                            String message = textMessage.getText().toString();
                
                            Intent email = new Intent(Intent.ACTION_SEND);
                            email.putExtra(Intent.EXTRA_EMAIL, new String[] { to });
                            // email.putExtra(Intent.EXTRA_CC, new String[]{ to});
                            // email.putExtra(Intent.EXTRA_BCC, new String[]{to});
                            email.putExtra(Intent.EXTRA_SUBJECT, subject);
                            email.putExtra(Intent.EXTRA_TEXT, message);
                
                            // need this to prompts email client only
                            email.setType("message/rfc822");
                
                            startActivity(Intent.createChooser(email, "Choose an Email client :"));
                
                        }
                    });
                }
                

                【讨论】:

                • 这与您发布此内容时已经存在的答案相比有什么更好的地方?它看起来就像是包含在活动中的已接受答案的副本。
                【解决方案16】:

                其他解决方案可以

                Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
                emailIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                emailIntent.setType("plain/text");
                emailIntent.setClassName("com.google.android.gm", "com.google.android.gm.ComposeActivityGmail");
                emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"someone@gmail.com"});
                emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Yo");
                emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Hi");
                startActivity(emailIntent);
                

                假设大多数 android 设备已经安装了 GMail 应用程序。

                【讨论】:

                • @PedroVarela 我们可以随时检查活动未找到异常。
                • 是的,我们可以。但是您的解决方案不正确。 Android 文档清楚地说明了您必须做什么才能在意图选择器中仅显示邮件应用程序。您写了“假设大多数 android 设备已经安装了 Gmail 应用程序”;如果它是 root 设备并且用户删除了 Gmail 客户端怎么办?假设您正在创建自己的电子邮件应用程序?如果用户要发送电子邮件,您的应用程序将不在该列表中。如果 gmail 更改包名会怎样?你要更新你的应用吗?
                【解决方案17】:

                用它来发送电子邮件...

                boolean success = EmailIntentBuilder.from(activity)
                    .to("support@example.org")
                    .cc("developer@example.org")
                    .subject("Error report")
                    .body(buildErrorReport())
                    .start();
                

                使用构建等级:

                compile 'de.cketti.mailto:email-intent-builder:1.0.0'
                

                【讨论】:

                  【解决方案18】:
                   Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
                              "mailto","ebgsoldier@gmail.com", null));
                      emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Forgot Password");
                      emailIntent.putExtra(Intent.EXTRA_TEXT, "this is a text ");
                      startActivity(Intent.createChooser(emailIntent, "Send email..."));
                  

                  【讨论】:

                    【解决方案19】:

                    这种方法对我有用。它打开 Gmail 应用程序(如果已安装)并设置 mailto。

                    public void openGmail(Activity activity) {
                        Intent emailIntent = new Intent(Intent.ACTION_VIEW);
                        emailIntent.setType("text/plain");
                        emailIntent.setType("message/rfc822");
                        emailIntent.setData(Uri.parse("mailto:"+activity.getString(R.string.mail_to)));
                        emailIntent.putExtra(Intent.EXTRA_SUBJECT, activity.getString(R.string.app_name) + " - info ");
                        final PackageManager pm = activity.getPackageManager();
                        final List<ResolveInfo> matches = pm.queryIntentActivities(emailIntent, 0);
                        ResolveInfo best = null;
                        for (final ResolveInfo info : matches)
                            if (info.activityInfo.packageName.endsWith(".gm") || info.activityInfo.name.toLowerCase().contains("gmail"))
                                best = info;
                        if (best != null)
                            emailIntent.setClassName(best.activityInfo.packageName, best.activityInfo.name);
                        activity.startActivity(emailIntent);
                    }
                    

                    【讨论】:

                      【解决方案20】:
                      /**
                       * Will start the chosen Email app
                       *
                       * @param context    current component context.
                       * @param emails     Emails you would like to send to.
                       * @param subject    The subject that will be used in the Email app.
                       * @param forceGmail True - if you want to open Gmail app, False otherwise. If the Gmail
                       *                   app is not installed on this device a chooser will be shown.
                       */
                      public static void sendEmail(Context context, String[] emails, String subject, boolean forceGmail) {
                      
                          Intent i = new Intent(Intent.ACTION_SENDTO);
                          i.setData(Uri.parse("mailto:"));
                          i.putExtra(Intent.EXTRA_EMAIL, emails);
                          i.putExtra(Intent.EXTRA_SUBJECT, subject);
                          if (forceGmail && isPackageInstalled(context, "com.google.android.gm")) {
                              i.setPackage("com.google.android.gm");
                              i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                              context.startActivity(i);
                          } else {
                              try {
                                  context.startActivity(Intent.createChooser(i, "Send mail..."));
                              } catch (ActivityNotFoundException e) {
                                  Toast.makeText(context, "No email app is installed on your device...", Toast.LENGTH_SHORT).show();
                              }
                          }
                      }
                      
                      /**
                       * Check if the given app is installed on this devuice.
                       *
                       * @param context     current component context.
                       * @param packageName The package name you would like to check.
                       * @return True if this package exist, otherwise False.
                       */
                      public static boolean isPackageInstalled(@NonNull Context context, @NonNull String packageName) {
                          PackageManager pm = context.getPackageManager();
                          if (pm != null) {
                              try {
                                  pm.getPackageInfo(packageName, 0);
                                  return true;
                              } catch (PackageManager.NameNotFoundException e) {
                                  e.printStackTrace();
                              }
                          }
                          return false;
                      }
                      

                      【讨论】:

                        【解决方案21】:

                        试试这个:

                        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
                        }
                        

                        上面的代码将打开用户最喜欢的电子邮件客户端,其中预填了准备发送的电子邮件。

                        Source

                        【讨论】:

                          【解决方案22】:

                          仅显示电子邮件客户端(无联系人等)的 Kotlin 版本:

                              with(Intent(Intent.ACTION_SEND)) {
                                  type = "message/rfc822"
                                  data = Uri.parse("mailto:")
                                  putExtra(Intent.EXTRA_EMAIL, arrayOf("example@mail.com"))
                                  putExtra(Intent.EXTRA_SUBJECT,"YOUR SUBJECT")
                                  putExtra(Intent.EXTRA_TEXT, "YOUR BODY")
                                  try {
                                      startActivity(Intent.createChooser(this, "Send Email with"))
                                  } catch (ex: ActivityNotFoundException) {
                                      // No email clients found, might show Toast here
                                  }
                              }
                          

                          【讨论】:

                            【解决方案23】:

                            以下代码适用于 Android 10 及更高版本的设备。它还设置了主题、正文和收件人(To)。

                            val uri = Uri.parse("mailto:$EMAIL")
                                            .buildUpon()
                                            .appendQueryParameter("subject", "App Feedback")
                                            .appendQueryParameter("body", "Body Text")
                                            .appendQueryParameter("to", EMAIL)
                                            .build()
                            
                                        val emailIntent = Intent(Intent.ACTION_SENDTO, uri)
                            
                                        startActivity(Intent.createChooser(emailIntent, "Select app"))
                            

                            【讨论】:

                              【解决方案24】:
                              import androidx.core.app.ShareCompat
                              import androidx.core.content.IntentCompat
                              
                              ShareCompat.IntentBuilder(this)
                                              .setType("message/rfc822")
                                              .setEmailTo(arrayOf(email))
                                              .setStream(uri)
                                              .setSubject(subject)
                                              .setText(message + emailMessage)
                                              .startChooser()
                              

                              【讨论】:

                                【解决方案25】:

                                这是在 Android 上发送电子邮件最简洁的方式。

                                 val intent = Intent(Intent.ACTION_SENDTO).apply {
                                    data = Uri.parse("mailto:")
                                    putExtra(Intent.EXTRA_EMAIL, arrayOf("email@gmail.com"))
                                    putExtra(Intent.EXTRA_SUBJECT, "Subject")
                                    putExtra(Intent.EXTRA_TEXT, "Email body")
                                }
                                if (intent.resolveActivity(packageManager) != null) {
                                    startActivity(intent)
                                }
                                

                                您还需要在您的 ma​​nifest(在您的应用程序标签之外)指定处理电子邮件 (mailto) 的应用程序的查询

                                <queries>
                                    <intent>
                                        <action android:name="android.intent.action.SENDTO" />
                                        <data android:scheme="mailto" />
                                    </intent>
                                </queries>
                                

                                如果您需要在电子邮件正文中发送 HTML 文本,请将“电子邮件正文”替换为您的电子邮件字符串,如下所示(请注意 Html.fromHtml 可能已弃用,这只是为了向您展示如何操作)

                                Html.fromHtml(
                                    StringBuilder().append("<b>Hello world</b>").toString()
                                )
                                

                                【讨论】:

                                  猜你喜欢
                                  • 1970-01-01
                                  • 1970-01-01
                                  • 2010-12-20
                                  • 2011-08-06
                                  相关资源
                                  最近更新 更多