Security Coding
Security Coding

Reputation: 177

Email Intent Value Not Set 'TO' : address

I Trying To Add My Email To the 'To' Address But It's putting in from address . How Can I Add My Email TO 'To' Address
In Android Studio

This My Code

` val btn: Button = findViewById(R.id.button)

    btn.setOnClickListener{
        val mailIntent = Intent(Intent.ACTION_SEND)
        mailIntent.type = "text/plain"
        mailIntent.putExtra(Intent.EXTRA_EMAIL,"[email protected]")
        mailIntent.putExtra(Intent.EXTRA_SUBJECT,"")
        mailIntent.putExtra(Intent.EXTRA_TEXT,"")
        startActivity(Intent.createChooser(mailIntent, "Send Email"))

`

Please Help Me How can SOlve This

Upvotes: 0

Views: 903

Answers (2)

quannm18
quannm18

Reputation: 21

I try mailIntent.putExtra(Intent.EXTRA_EMAIL, arrayOf("[email protected]")) and It works for me

Upvotes: 0

Karmveer Singh
Karmveer Singh

Reputation: 341

val mailIntent = Intent(Intent.ACTION_SEND)

Instead of using Intent.ACTION_SEND, you can use Intent.ACTION_SENDTO This will only list the e-mail clients for you.

Moreover, see this answer for more clarity and updated answer

https://stackoverflow.com/a/15022153/13597058

Update :

send_mail.setOnClickListener {
            val i = Intent(Intent.ACTION_SEND)
            i.type = "message/rfc822"
            i.putExtra(Intent.EXTRA_EMAIL, arrayOf("[email protected]"))
            i.putExtra(Intent.EXTRA_SUBJECT, "subject of email")
            i.putExtra(Intent.EXTRA_TEXT, "body of email")
            try {
                startActivity(Intent.createChooser(i, "Send mail..."))
            } catch (ex: ActivityNotFoundException) {
                Toast.makeText(
                    this,
                    "There are no email clients installed.",
                    Toast.LENGTH_SHORT
                ).show()
            }
        }

This code solves your problem.

enter image description here

Upvotes: 2

Related Questions