Home > front end >  I want to send a message by email but the Intent doesn't work. Why?
I want to send a message by email but the Intent doesn't work. Why?

Time:11-24

There is a part of my app where I send an email using a button but for some reason the Intent doesn't work and I don't understand why.

binding.IvMail.setOnClickListener {
    val email = Intent(Intent.ACTION_SEND)
        .setType("text/plain")
        .putExtra(Intent.EXTRA_SUBJECT, getString(R.string.email_subject))
        .putExtra(Intent.EXTRA_TEXT, getString(R.string.email_text))

    if (activity?.packageManager?.resolveActivity(email, 0) != null) {
        startActivity(email)
    }

}

I already searched for other ways to do it but everyone is using Intent.

CodePudding user response:

         // try this(JAVA).
        Intent i = new Intent(Intent.ACTION_SEND);
        i.setType("message/rfc822");
        i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"[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 (android.content.ActivityNotFoundException ex) {
        Toast.makeText(MyActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
    }
    ////For(Kotlin)
        sendEmail(recipient, subject, message)

 private fun sendEmail(recipient: String, subject: String, message: 
String) {
        /*ACTION_SEND action to launch an email client installed on your Android device.*/
        val mIntent = Intent(Intent.ACTION_SEND)
        /*To send an email you need to specify mailto: as URI using 
setData() method
        and data type will be to text/plain using setType() method*/
        mIntent.data = Uri.parse("mailto:")
        mIntent.type = "text/plain"
        // put recipient email in intent
        /* recipient is put as array because you may wanna send email to multiple emails
           so enter comma(,) separated emails, it will be stored array*/
        mIntent.putExtra(Intent.EXTRA_EMAIL, arrayOf(recipient))
        //put the Subject in the intent
        mIntent.putExtra(Intent.EXTRA_SUBJECT, subject)
        //put the message in the intent
        mIntent.putExtra(Intent.EXTRA_TEXT, message)


        try {
            //start email intent
            startActivity(Intent.createChooser(mIntent, "Choose Email Client..."))
        }
        catch (e: Exception){
            //if any thing goes wrong for example no email client application or any exception
            //get and show exception message
            Toast.makeText(this, e.message, Toast.LENGTH_LONG).show()
        }

    }
}

CodePudding user response:

You have to use ACTION_SENDTO instead of ACTION_SEND.

See your new code below:

binding.IvMail.setOnClickListener {
    val email = Intent(Intent.ACTION_SENDTO) // here you have to use SENDTO
        .setType("text/plain")
        .putExtra(Intent.EXTRA_SUBJECT, getString(R.string.email_subject))
        .putExtra(Intent.EXTRA_TEXT, getString(R.string.email_text))

    if (activity?.packageManager?.resolveActivity(email, 0) != null) {
        startActivity(email)
    }

}

Let me know if not solved yet

  • Related