0

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,"ramanathank18@gmail.com")
        mailIntent.putExtra(Intent.EXTRA_SUBJECT,"")
        mailIntent.putExtra(Intent.EXTRA_TEXT,"")
        startActivity(Intent.createChooser(mailIntent, "Send Email"))

`

Please Help Me How can SOlve This

Security Coding
  • 119
  • 1
  • 9
  • 1
    Note that there is no requirement for an app that responds to `ACTION_SEND` to use `EXTRA_EMAIL` or otherwise let you pre-populate a "To" field. You can *offer* the email address via `EXTRA_EMAIL`, but what the other app does is up to the developers of the other app. – CommonsWare May 14 '21 at 11:26

1 Answers1

2
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("recipient@example.com"))
            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

Karmveer Singh
  • 291
  • 3
  • 14