Georgina Bennett
Georgina Bennett

Reputation: 51

Mailto Android: 'Unsupported action' error

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!

Upvotes: 5

Views: 6048

Answers (3)

pokumars
pokumars

Reputation: 161

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.

Upvotes: -1

Sam
Sam

Reputation: 42427

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);

Upvotes: 14

Abhinav Puri
Abhinav Puri

Reputation: 4284

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 :"));
    }});

}

Upvotes: 1

Related Questions