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


87

我正在尝试使用 从我的应用程序发送电子邮件,但不会填充电子邮件的“收件人”字段。如果我添加代码以填写主题或文本,则它们可以正常工作。仅“收件人”字段将不会填充。

我也尝试过将类型更改为“ text / plain”和“ 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

Answers:


213

我想你不及格recipientarray of string

它应该像

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

19
Android ...你为什么这么可悲?
错误发生

4
哈哈哈,你让我笑了+1百万@BugsHappen ..原因:它是开源的,但文档并不能100%令人满意,设备制造商会根据需要进行修改(大多数设备便宜且无用),开发人员不会阅读“开发人员”。 android.com”。
MKJParekh '16

4
还要确保您会这样做intent.putExtra(Intent.EXTRA_EMAIL, list.toArray()) ,因为list.toArray()会产生Object []而不是String []
nikib3ro

@ kape123这样做的工作不过intent.putExtra(Intent.EXTRA_EMAIL, list.toArray(new String[0]))
Abtin Gramian矩阵

4

用这个

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#Emailhttp://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);
}

2
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:"));
}

就是说没有应用可以执行此操作。
阿布·纳耶姆

2

在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开发人员有所帮助。


1

几件事情:

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"});
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.