ACTION_SENDTO does not work
Asked Answered
C

5

6

I want to send out an email from my app. So I used the following code.

String uriText = "[email protected]" + "?subject=" + URLEncoder.encode("Subject") + "&body=" + URLEncoder.encode("some text here");
Uri uri = Uri.parse(uriText);
Intent sendIntent = new Intent(Intent.ACTION_SENDTO);
sendIntent.setData(uri);
startActivity(Intent.createChooser(sendIntent, "Send Email"));

I have configured both my Gmail and EMail applications. I tested on my Nexus S (JellyBean) and HTC T-Mobile G2 (GingerBread). Both of them shows "No apps can perform this action.".

Does anyone have idea what's wrong here?

Crossways answered 21/12, 2012 at 17:44 Comment(0)
P
13

If you are going to use ACTION_SENDTO, the Uri should use the mailto: or smsto: scheme. So, try mailto:[email protected].

Pippin answered 21/12, 2012 at 17:46 Comment(2)
@KarthikPalanivelu: I do not understand. If "the normal activity" is the chooser, that should go away on its own. If "the normal activity" is your activity, you can can finish() on it to make it go away. If "the normal activity" is something else, then there probably is nothing you can do about it.Pippin
Am really sorry. It was a simple mistake on my side. I dint have a break statement where i had to. So, it executed the following code and another ACTION_SEND activity was launched. I noticed immediately so deleted the comment right away. Thanks for your reply :)Crossways
L
9

if you are using Intent.setData for sending email then change your code as:

String uriText = "mailto:[email protected]" +
                 "?subject=" + URLEncoder.encode("Subject") + 
                 "&body=" + URLEncoder.encode("some text here");
Uri uri = Uri.parse(uriText);
Intent sendIntent = new Intent(Intent.ACTION_SENDTO);
sendIntent.setData(uri);
startActivity(Intent.createChooser(sendIntent, "Send Email"));
Lilybel answered 21/12, 2012 at 17:50 Comment(0)
A
1

Uri should be "mailto"

 Intent intent = new Intent(Intent.ACTION_SENDTO);  
 intent.setData(Uri.parse("mailto:"));  
 intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"[email protected]"});  
 intent.putExtra(Intent.EXTRA_SUBJECT,"Order summary of Coffee");
 intent.putExtra(Intent.EXTRA_TEXT,BodyOfEmail);

 if(intent.resolveActivity(getPackageManager())!=null) {
            startActivity(intent);
        }
Ankle answered 15/7, 2020 at 19:56 Comment(0)
M
0

I've tried all day to open the com.android.mms/.ui.ComposeMessageActivity (Huawei).
While it's indeed pretty simple and it seemingly does not need any special handling:

try { startActivity(Intent(Intent.ACTION_SENDTO, Uri.parse("mmsto:"))) }
catch (e: Exception) { Log.e(LOG_TAG, "Exception: ${e.message}") }
Matchmaker answered 2/10, 2024 at 23:48 Comment(0)
T
-1

The following code worked for me and is a lot more reliable and flexible. Also, it's written in Kotlin :)

fun sendEmail(context: Context, mailTo: String? = null, subject: String? = null, bodyText: String? = null) {
    val emailIntent = Intent(Intent.ACTION_SENDTO)
    val uri = Uri.parse("mailto:${mailTo ?: ""}").buildUpon() // "mailto:" (even if empty) is required to open email composers
    subject?.let { uri.appendQueryParameter("subject", it) }
    bodyText?.let { uri.appendQueryParameter("body", it) }
    emailIntent.data = uri.build()
    try {
        context.startActivity(emailIntent)
    } catch (e: ActivityNotFoundException) {
        // Handle error properly
    }
}
Turgite answered 28/11, 2018 at 21:21 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.