0

I implemented sending email feature on my app like this:

val emailIntent = Intent(Intent.ACTION_SENDTO)
emailIntent.type = "message/rfc822"
emailIntent.putExtra(Intent.EXTRA_EMAIL, arrayOf("myId@gmail.com"))
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Hello~")
emailIntent.putExtra(Intent.EXTRA_TEXT, "$sendData")

try {
    startActivityForResult(Intent.createChooser(emailIntent, "Send mail..."), RESULT_SEND_EMAIL)
} catch (ex: ActivityNotFoundException) {
    Toast.makeText(this@MyActivity, "There are no email clients installed.", Toast.LENGTH_SHORT).show()
} catch (e : Exception){
    Toast.makeText(this@MyActivity, "${e.toString()}", Toast.LENGTH_SHORT).show()
}

I used startActivityForResult() for the process after coming back from the intent.

And the problem is Gmail app doesn't open on one of my coworker's device. She uses Huawei mate pro. It still shows up on the app chooser menu. But just can't open.

What's wrong with this? Is it my problem or Gmail problem or the device problem?

c-an
  • 3,543
  • 5
  • 35
  • 82
  • 1
    Use `startActivity()` instead `startActivityForResult()` – Pratik Butani Mar 16 '20 at 05:21
  • Is there any reason? @PratikButani I wanna call some function after the result. – c-an Mar 16 '20 at 05:28
  • @PratikButani What? This isn't duplicate with https://stackoverflow.com/questions/3470042/intent-uri-to-launch-gmail-app. – c-an Mar 16 '20 at 05:34
  • startActivityForResult() works totally fine with other devices. – c-an Mar 16 '20 at 05:35
  • Remove this line `emailIntent.type = "message/rfc822"` and try again. – Pratik Butani Mar 16 '20 at 05:47
  • This is [duplicate](https://stackoverflow.com/q/8701634/1318946) actually – Pratik Butani Mar 16 '20 at 05:49
  • Why should I remove `emailIntent.type = "message/rfc822"`? Some devices don't work with that type? It worked well with most of the devices. – c-an Mar 16 '20 at 05:51
  • This isn't duplicate because it is only for some devices' issue. @PratikButani the question is talking about How to make Camera app. And I am talking about I get 90 degree rotated on some devices, how can I fix it? And in the link, it doesn't contain enough information I wanna get, 'Why should I use startActivity() instead of startActivityForResult()?' and why do some devices have a problem if I use rfc822 type? If your answers were 'give it a try' question without any clarification, and you flagged this 'duplicate', It makes no sense. – c-an Mar 16 '20 at 05:58
  • I don't think so, by the way, I removed the duplicate flag. Your simple answer is your device has a problem. – Pratik Butani Mar 16 '20 at 06:03
  • 1
    I've tried all and All of your answers were incorrect. It works fine with `startActivityForResult()` and type `message/rfc822`. @PratikButani – c-an Mar 16 '20 at 11:38

1 Answers1

0

I found the solution in the document.

You need to use mailto: scheme. That allows you open the email apps. In other words, All mail app should have intent filter with the scheme to open your email application. If you don't include it, it won't work properly.

The example in the doc is the best example.

    fun composeEmail(addresses: Array<String>, subject: String, body: 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)
            putExtra(Intent.EXTRA_TEXT, body)
        }

        if (intent.resolveActivity(packageManager) != null) {
            try {
                startActivity(Intent.createChooser(intent, "Send mail...")))
            } catch (ex: ActivityNotFoundException) {
                Toast.makeText(this@MainActivity, "There are no email clients installed.", Toast.LENGTH_SHORT).show()
            }
        }
    }

Usage:

composeEmail(arrayOf("youremail@gmail.com", subject, body)

You can use startActivityForResult() if you want to do some work after closing the Email application. (However, there's no way you can catch the result of sending was successful or not because ACTION_SENDTO and ACTION_SEND returns nothing which means, it has no big difference with startActivity().

If you want to send an email to more than one person. You have to add more people in the array that is why the composeEmail method takes array. Here, you might want to know the differences between ACTION_SEND and ACTION_SENDTO. ACTION_SEND is with attachment and ACTION_SENDTO is for without attachment. If you want multiple attachment, than you have to use ACTION_SEND_MULIPLE.

c-an
  • 3,543
  • 5
  • 35
  • 82