Jon Arruabarrena
Jon Arruabarrena

Reputation: 3

Send mail programmatically Android

I am using this to send email programmatically in android but in Android 4.4.4 works bad. Is there another way to do this? Here is my code, thank you.

Intent i = new Intent(Intent.ACTION_SEND);  
//i.setType("text/plain"); //use this line for testing in the emulator  
i.setType("message/rfc822") ; // use from live device

i.putExtra(Intent.EXTRA_SUBJECT,"-my app-");  
i.putExtra(Intent.EXTRA_TEXT,"Hello");  
startActivity(Intent.createChooser(i, "Select your mail app"));

Upvotes: 0

Views: 272

Answers (2)

CommonsWare
CommonsWare

Reputation: 1007533

The dialog appears very big on the screen

The size of the chooser window is up to the device, not you. The chooser window will be the same size for all apps on the device that trigger a chooser, and so the user will be expecting to see the "very big" chooser window on devices that have one.

If you feel that the size of the chooser window should be what you want rather than what your users will expect, you will need to create your own chooser. You can do this using PackageManager and queryIntentActivities() to see what all responds to your Intent and using that to populate some chooser UI of your own design.

Upvotes: 1

Ganesh Katikar
Ganesh Katikar

Reputation: 2700

I hope below code will be helpfull for you..

protected void sendEmail() {

      String[] recipients = {recipient.getText().toString()};
      Intent email = new Intent(Intent.ACTION_SEND, Uri.parse("mailto:"));

      // prompts email clients only
      email.setType("message/rfc822");
      email.putExtra(Intent.EXTRA_EMAIL, recipients);
      email.putExtra(Intent.EXTRA_SUBJECT, subject.getText().toString());
      email.putExtra(Intent.EXTRA_TEXT, body.getText().toString());
      try {
        // the user can choose the email client
         startActivity(Intent.createChooser(email, "Choose an email client from..."));
      } catch (android.content.ActivityNotFoundException ex) {
         Toast.makeText(MainActivity.this, "No email client installed.",
                 Toast.LENGTH_LONG).show();
      }

   }

Above code works for me! Enjoy!!!

Upvotes: 0

Related Questions