【问题标题】:How to Send Emails?如何发送电子邮件?
【发布时间】:2018-05-31 20:58:52
【问题描述】:

我找到了一种通过 Android 应用发送电子邮件的算法。当我运行它时,它会打开一个应用列表,我可以选择发送它,但是,我只想使用 Gmail 应用。

这是代码:

protected void sendEmail(String subject, String text) {
    Log.i("Send email", "");
    String[] TO = {"email@gmail.com"};

    Intent emailIntent = new Intent(Intent.ACTION_SEND);

    emailIntent.setData(Uri.parse("mailto:"));
    emailIntent.setType("message/rfc822");
    emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
    emailIntent.putExtra(Intent.EXTRA_TEXT, text);

    try {
        startActivity(Intent.createChooser(emailIntent, "Send mail..."));
        finish();
        Log.i("Finished sending email!", "");
    } catch (android.content.ActivityNotFoundException ex) {
        Toast.makeText(getApplicationContext(), "There is no email client installed.", Toast.LENGTH_SHORT).show();
    }
}

提前致谢!

【问题讨论】:

  • “我只想使用 Gmail 应用”——您的用户想要使用什么?如果他们不想使用 Gmail 怎么办?
  • 可能他正在制作一个供自己使用的应用程序! @CommonsWare
  • @CommonsWare 用于问题报告功能。它有一个预先确定的电子邮件帐户,用于发送电子邮件。目前,它允许 Gmail、标准电子邮件应用、Whatsapp 和 Google Drive,这在这种情况下没有意义。

标签: android email gmail


【解决方案1】:

Implicit Intents 声明要执行的一般操作,在您的情况下,每个应用程序都具有电子邮件客户端的 intent 过滤器,这可以是 Gmail、Outlook 或您的设备拥有的任何电子邮件客户端。

在这种情况下你可以试试这个代码:

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

【讨论】:

  • 这是未记录的,不受支持的,并且将来可能会中断。欲了解更多信息,请参阅this
【解决方案2】:

您可以使用以下代码:

 Intent mailClient = new Intent(Intent.ACTION_VIEW);
 mailClient.setClassName("com.google.android.gm", 
 "com.google.android.gm.ConversationListActivity");
 startActivity(mailClient);

【讨论】:

  • 此解决方案未记录且不受支持。
猜你喜欢
  • 1970-01-01
  • 2017-01-16
  • 2019-01-09
  • 2011-01-01
  • 2012-07-22
  • 2020-01-24
  • 1970-01-01
相关资源
最近更新 更多