【问题标题】:Android - Opening the email application?Android - 打开电子邮件应用程序?
【发布时间】:2011-03-16 23:38:41
【问题描述】:

我想在我的安卓应用上打开电子邮件应用: 以下代码崩溃 我做错什么了吗?请提供代码

Intent i = new Intent (Intent.ACTION_SEND,Uri.fromParts("mailto", "testemail@gmail.com", null));
this.startActivity(i);

【问题讨论】:

  • 在 Eclipse 中使用 adb logcat、DDMS 或 DDMS 透视图检查 LogCat 并查看与您的崩溃相关的堆栈跟踪。

标签: android email android-activity android-intent


【解决方案1】:
/* Create the Intent */
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);

/* Fill it with Data */
emailIntent.setType("plain/text");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"to@email.com"});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Text");

/* Send it off to the Activity-Chooser */
context.startActivity(Intent.createChooser(emailIntent, "Send mail..."));

试试这个,它更清楚一点。尽管如此,仅当您在真机中使用该应用程序时,电子邮件意图才有效,因此如果您使用的是模拟器,请在真机上尝试。

【讨论】:

  • 如果你想正确,请使用emailIntent.setType("message/rfc822");
  • 如果没有安装任何电子邮件应用程序怎么办?此代码会打开浏览器并重定向到 google mail 之类的电子邮件站点吗?
  • 添加这些以确保选择器仅显示电子邮件应用程序:Intent i = new Intent(Intent.ACTION_SENDTO); i.setType("message/rfc822");i.setData(Uri.parse("mailto:"));
  • @priwiljay 的解决方案完全符合我的需要。我缺少的是最后一件事,即添加 i.setData(Uri.parse("mailto:"));
  • 需要加emailIntent.data = Uri.parse("mailto:")
【解决方案2】:

试试这个:

    Intent intent = new Intent(Intent.ACTION_VIEW);
    Uri data = Uri.parse("mailto:"
            + "xyz@abc.com"
            + "?subject=" + "Feedback" + "&body=" + "");
    intent.setData(data);
    startActivity(intent);

【讨论】:

    【解决方案3】:

    如果可以的话,最好使用常量,比如intent.type ClipDescription.MIMETYPE_TEXT_PLAIN

    科特林:

    val intent = Intent(Intent.ACTION_SEND)
    intent.type = ClipDescription.MIMETYPE_TEXT_PLAIN
    intent.putExtra(Intent.EXTRA_EMAIL, arrayOf("emailId 1", "emailId 2"))
    intent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Subject for email")
    intent.putExtra(android.content.Intent.EXTRA_TEXT, "Description for email")
    startActivity(Intent.createChooser(intent,"Send Email"))
    

    Java:

    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType(ClipDescription.MIMETYPE_TEXT_PLAIN);
    intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"emailId 1", "emailId 2"});
    intent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Subject for email");
    intent.putExtra(android.content.Intent.EXTRA_TEXT, "Description for email");
    startActivity(Intent.createChooser(intent,"Send Email"));
    

    【讨论】:

      【解决方案4】:
      Intent intent = new Intent(Intent.ACTION_SEND);
      intent.setType("plain/text");
      intent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] {});
      intent.putExtra(android.content.Intent.EXTRA_SUBJECT,"");
      intent.putExtra(android.content.Intent.EXTRA_TEXT, "");
      
      /* Send it off to the Activity-Chooser */
      startActivity(Intent.createChooser(intent,"Send"));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-01-31
        • 2012-07-26
        • 1970-01-01
        • 1970-01-01
        • 2011-01-03
        • 1970-01-01
        • 2012-07-30
        相关资源
        最近更新 更多