Intent.EXTRA_EMAIL未填充“收件人”字段



我正试图使用从应用程序发送电子邮件的意图,但电子邮件的"收件人"字段不会填充。如果我添加代码来填充主题或文本,它们会很好地工作。不会仅填充"收件人"字段。

我也尝试过将类型更改为"text/plain"one_answers"text/html",但我遇到了同样的问题。有人能帮忙吗?

public void Email(){
    Intent emailIntent = new Intent(Intent.ACTION_SEND); 
    emailIntent.setType("message/rfc822");  //set the email recipient
    String recipient = getString(R.string.IntegralEmailAddress);
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL  , recipient);
    //let the user choose what email client to use
    startActivity(Intent.createChooser(emailIntent, "Send mail using...")); }

我尝试使用的电子邮件客户端是Gmail

我认为您没有将recipient作为array of string 传递

它应该像

emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,new String[] { "someone@gmail.com" });

在Kotlin-Android

fun sendMail(
        activity: Activity,
        emailIds: Array<String>,
        subject: String,
        textMessage: String
    ) {

        val emailIntent = Intent(Intent.ACTION_SEND)
        emailIntent.type = "text/plain"
        emailIntent.putExtra(Intent.EXTRA_EMAIL, emailIds)
        emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject)
        emailIntent.putExtra(Intent.EXTRA_TEXT, textMessage)
        emailIntent.setType("message/rfc822")
        try {
            activity.startActivity(
                Intent.createChooser(
                    emailIntent,
                    "Send email using..."
                )
            )
        } catch (ex: ActivityNotFoundException) {
            Toast.makeText(
                activity,
                "No email clients installed.",
                Toast.LENGTH_SHORT
            ).show()
        }
    }

您还可以使用[val emailIntent = Intent(Intent.ACTION_SENDTO)]调用直接电子邮件客户端

//argument of function
val subject = "subject of you email"
val eMailMessageTxt = "Add Message here"
val eMailId1 = "emailId1@gmail.com"
val eMailId2 = "emailId2@gmail.com"
val eMailIds: Array<String> = arrayOf(eMailId1,eMailId2)
//Calling function
sendMail(this, eMailIds, subject, eMailMessageTxt)

我希望这个代码片段能对kotlin开发人员有所帮助。

使用此

public void Email(){
    // use this to declare your 'recipient' string and get your email recipient from your string xml file
    Resources res = getResources();
    String recipient = getString(R.string.IntegralEmailAddress);
    Intent emailIntent = new Intent(Intent.ACTION_SEND); 
    emailIntent.setType("message/rfc822");  //set the email recipient
    emailIntent.putExtra(Intent.EXTRA_EMAIL, recipient);
    //let the user choose what email client to use
    startActivity(Intent.createChooser(emailIntent, "Send mail using...")); 
``}

这将起作用:)
这是android文档对Intent.Extra_Email的描述
-所有"收件人"电子邮件地址的字符串数组
所以你们应该适当地喂绳子您可以在此处阅读更多信息
http://developer.android.com/guide/components/intents-common.html#Email在这里http://developer.android.com/guide/topics/resources/string-resource.html或者使用ACTION_SENDTO操作并包含"mailto:"数据方案。例如:

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

几件事:

1-您需要将动作常量变量设置为action_SENDTO
Intent intentEmail = new Intent(Intent.ACTION_SENDTO);

2-如果你想只通过邮件打开它,那么使用setData()方法:intentEmail.setData(Uri.parse("mailto:"));否则它会要求你通过设备上的其他应用程序以文本、图像、音频文件的形式打开它

3-您需要将电子邮件ID字符串作为数组对象传递,而不仅仅是作为字符串传递。字符串为:"name@email.com".字符串的数组对象为:new string[]{"email1","email2","more_email"}.

intentEmail.putExtra(Intent.EXTRA_EMAIL, new String[] {"email@overflow.com", "abcd@stack.com"});
private void callSendMeMail() {
    Intent Email = new Intent(Intent.ACTION_SEND);
    Email.setType("text/email");
    Email.putExtra(Intent.EXTRA_EMAIL, new String[] { "me@gmail.com" });
    Email.putExtra(Intent.EXTRA_SUBJECT, "Feedback");
    startActivity(Intent.createChooser(Email, "Send mail to Developer:"));
}

让我浪费了这么多时间!感谢被接受的答案!我将添加一些Kotlin代码和一些方便的扩展功能

fun Activity.hasEmailClient(): Boolean =
    emailIntent("someAddress", "someSubject", "someText")
        .resolveActivity(packageManager) != null
fun Activity.openEmailClient(address: String, subject: String, text: String) {
    startActivity(emailIntent(address, subject, text))
}
private fun emailIntent(address: String, subject: String, text: String): Intent =
    Intent(Intent.ACTION_SENDTO).apply {
        type = "text/plain"
        data = Uri.parse("mailto:")
        putExtra(Intent.EXTRA_EMAIL, arrayOf(address))
        putExtra(Intent.EXTRA_SUBJECT, subject)
        putExtra(Intent.EXTRA_TEXT, text)
    }

如果"活动"适合您的需要,请随时将其替换为"片段"。示例用法:

if (hasEmailClient()) {
    openEmailClient(
        "example@email.info",
        "this is the subject",
        "this is the text"
    )
}

这就是适用于我的:

val email = "recipient@email.com"
val intent = Intent(Intent.ACTION_SENDTO)
intent.data = Uri.parse("mailto:$email")
intent.putExtra(Intent.EXTRA_SUBJECT,"My Subject")
startActivity(intent)

首先,您应该将Intent.EXTRA_EMAIL的值设置为字符串数组类型,如下所示:

emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { "some@gmail.com" });

如果不起作用,只需卸载应用程序,然后再次运行。。。。

      Intent intent = new Intent(Intent.ACTION_SENDTO);
            intent.setData(Uri.parse("mailto:")); 
            intent.putExtra(Intent.EXTRA_EMAIL, new String[] { "xyx@gmail.com" });
            intent.putExtra(Intent.EXTRA_SUBJECT, "subject");
            if (intent.resolveActivity(getPackageManager()) != null) {
                startActivity(intent);
            }

最新更新