用于发送电子邮件的ACTION_SENDTO

时间:2010-06-28 13:47:17

标签: android email android-intent

在Android 2.1中执行以下代码段时,我遇到“当前不支持此操作”错误情况。这个片段有什么问题?

public void onClick(View v) {
  Intent intent = new Intent(Intent.ACTION_SENDTO);
  Uri uri = Uri.parse("mailto:myemail@gmail.com");
  intent.setData(uri);
  intent.putExtra("subject", "my subject");
  intent.putExtra("body", "my message");
  startActivity(intent);
}

3 个答案:

答案 0 :(得分:80)

如果您使用ACTION_SENDTO,则putExtra()无法将主题和文字添加到意图中。使用setData()Uri工具添加主题和文字。

此示例适用于我:

// ACTION_SENDTO filters for email apps (discard bluetooth and others)
String uriText =
    "mailto:youremail@gmail.com" + 
    "?subject=" + Uri.encode("some subject text here") + 
    "&body=" + Uri.encode("some text here");

Uri uri = Uri.parse(uriText);

Intent sendIntent = new Intent(Intent.ACTION_SENDTO);
sendIntent.setData(uri);
startActivity(Intent.createChooser(sendIntent, "Send email")); 

答案 1 :(得分:19)

实际上我找到了EXTRAs工作的方法。 这是我在这里找到关于ACTION_SENDTO

的答案的组合

http://www.coderanch.com/t/520651/Android/Mobile/no-application-perform-action-when

以及我在这里找到的HTML内容:

How to send HTML email (但是在这篇HTML帖子中如何使用ACTION_SENDTO的变体对我不起作用 - 我得到了“你不支持的动作”)

    // works with blank mailId - user will have to fill in To: field
    String mailId="";
    // or can work with pre-filled-in To: field
// String mailId="yourmail@gmail.com";
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, 
                                    Uri.fromParts("mailto",mailId, null)); 
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject text here"); 
    // you can use simple text like this
// emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,"Body text here"); 
    // or get fancy with HTML like this
emailIntent.putExtra(
             Intent.EXTRA_TEXT,
             Html.fromHtml(new StringBuilder()
                 .append("<p><b>Some Content</b></p>")
                 .append("<a>http://www.google.com</a>")
                 .append("<small><p>More content</p></small>")
                 .toString())
             );
startActivity(Intent.createChooser(emailIntent, "Send email..."));

答案 2 :(得分:4)

你可以尝试这个

 Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
            "mailto", emailID, null));
    emailIntent.putExtra(Intent.EXTRA_SUBJECT,
            Subject);
    emailIntent.putExtra(Intent.EXTRA_TEXT,
            Text);
    startActivity(Intent.createChooser(emailIntent, Choosertitle);

对我有用