Mailto Android: 'Unsupported action' error
Asked Answered
G

3

5

I am new to this but what is wrong with my snippet of coding? I am getting the error: 'This action is not currently supported' when I select the link. Here is my code:

public void addEmail() {

    TextView txt = (TextView) findViewById(R.id.emailtext);

    txt.setOnClickListener(new View.OnClickListener(){


        public void onClick(View v){
            Intent intent = new Intent();
            String uriText =
                    "mailto:[email protected]" + 
                    "?subject=" + URLEncoder.encode("some subject text here") + 
                    "&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")); 

    }});

}

Many thanks!

Girosol answered 17/12, 2014 at 14:48 Comment(6)
@see #2198241Solicit
@shkschneider, how does that address the error?Choric
I ran into this in the Android 4.0.2 emulator when I hadn't set up an email account yet in the Android email client. Setting up an account in the client worked around the problem.Choric
@Choric Doesn't it explain how to correctly send an email using an Intent? It appears the answer below (that you confirmed) goes the same direction. This is how I thought my link addressed the error. My bad if it does not.Solicit
@shkschneider, it does explain how to send emails, but it doesn't really address the actual cause of the "Unsupported action" error. The code in the question actually works fine, but it just breaks in this way in specific circumstances, which, from my testing so far, seem to be a combination of: using an emulator, using setData, and the intent not matching anything. The code in this question actually seems better than the setType approach since, according to comments on some SO answers, that approach causes Skype and other non-email clients to match the intent.Choric
@San I agree. But that's why I posted a comment without pretending to provide an answer. Regards.Solicit
C
14

The problem is probably that you're running on one of the official Android emulators and you haven't yet set up an email account on it. The emulators open the com.android.fallback.Fallback activity when this happens, but this doesn't seem to happen on real-world devices.

You can detect this before trying to start the intent using this code:

ComponentName emailApp = intent.resolveActivity(getPackageManager());
ComponentName unsupportedAction = ComponentName.unflattenFromString("com.android.fallback/.Fallback");
boolean hasEmailApp = emailApp != null && !emailApp.equals(unsupportedAction);
Choric answered 25/6, 2015 at 10:59 Comment(0)
H
1

Try this, it worked for me :

public void addEmail() {

     TextView txt = (TextView) findViewById(R.id.emailtext);

     txt.setOnClickListener(new View.OnClickListener(){

     public void onClick(View v){

            String[] emails = {"[email protected]"};
            String subject = "your subject";
            String message = "your message";

            Intent email = new Intent(Intent.ACTION_SEND);
            email.putExtra(Intent.EXTRA_EMAIL, emails);
            email.putExtra(Intent.EXTRA_SUBJECT, subject);
            email.putExtra(Intent.EXTRA_TEXT, message);

            // need this to prompts email client only
            email.setType("message/rfc822");

            startActivity(Intent.createChooser(email, "Choose an Email client :"));
    }});

}
Hurried answered 17/12, 2014 at 15:15 Comment(1)
I've tested and confirmed that this works. Looks like the problem was caused by using setData() when no apps match the intent.Choric
H
-1

The problem is because you havent set up an email account on the emulator. I had the same problem on the emulator but not any more when I tested it on phone.

Holyhead answered 9/5, 2018 at 22:28 Comment(1)
This answer simply reiterates what has already been stated in the other answers and comments.Segal

© 2022 - 2024 — McMap. All rights reserved.