1

I have the following intent -

binding.fragmentContactUsEmail.setOnClickListener {
            val intent = Intent(Intent.ACTION_SEND).apply {
                type = "*/*"
                putExtra(Intent.EXTRA_EMAIL, EMAIL)
                putExtra(Intent.EXTRA_SUBJECT, "Team-It application feedback")
            }
            if (intent.resolveActivity(requireActivity().packageManager) != null) {
                startActivity(intent)
            }
        }

Which does indeed open email intent.

The issue is that when trying to filter the intent using the docs from developer.android.com like this -

fun composeEmail(addresses: Array<String>, subject: String) {
    val intent = Intent(Intent.ACTION_SENDTO).apply {
        data = Uri.parse("mailto:") // only email apps should handle this
        putExtra(Intent.EXTRA_EMAIL, addresses)
        putExtra(Intent.EXTRA_SUBJECT, subject)
    }
    if (intent.resolveActivity(packageManager) != null) {
        startActivity(intent)
    }
}

It failed the if statement like I do not have any email applications in my device, which is wrong as I do have one of them.

Another issue is that I can't get the Intent.EXTRA_EMAIL to work - it just does not add the e-mail addresses that should be delivered to, it leaves it blank.

What am I missing?

edit -

After trying a solution given to me in the comments, I was able to get the email send to an address but still title does not work.

Here is my current code -

binding.fragmentContactUsEmail.setOnClickListener {
            val emailIntent = Intent(Intent.ACTION_SENDTO).apply {
                data = Uri.parse("mailto:${Constants.ApplicationContactInformation.EMAIL}")
                putExtra(Intent.EXTRA_SUBJECT, "Feedback for Team-It")
            }
            if (emailIntent.resolveActivity(requireActivity().packageManager) != null) {
                startActivity(Intent.createChooser(emailIntent, ""))
            } else {
                Toast.makeText(requireContext(), getString(R.string.contact_us_fragment_no_email_applications), Toast.LENGTH_SHORT).show();
            }
        }
Alon Shlider
  • 1,187
  • 1
  • 16
  • 46
  • 1
    Check out this [link](https://stackoverflow.com/questions/8701634/send-email-intent). Hope it is helpful. – Tugay Aug 11 '20 at 11:31
  • @Tuqay thank you - it solved the issue of the email receiver. Now I still have the subject issue unsolved, please take a look. – Alon Shlider Aug 11 '20 at 13:53
  • I checked the docs once again and I can't see any problem with your implementation. Check this [answer](https://stackoverflow.com/a/30227283/11199298). If it doesn't help too, then delete this post and post a new one on this specific issue. – Tugay Aug 11 '20 at 14:37

1 Answers1

0

try this code :

  Intent email = new Intent(Intent.ACTION_SEND);  
                  email.putExtra(Intent.EXTRA_EMAIL, new String[]{ "to"});  
                  email.putExtra(Intent.EXTRA_SUBJECT, "subject");  
                  email.putExtra(Intent.EXTRA_TEXT, "hi..");  
       
                  //need this to prompts email client only  
                  email.setType("message/rfc822");  
       
                  startActivity(Intent.createChooser(email, "Choose an Email client :"));