-1

I am trying to create an activity to send an email. I am using an Intent Object "Action send" to launch the email client.

However is not detecting the email client, this is the first time I am doing this, please help. What is wrong with my code?

public class email extends Activity {

    private Button send;
    DBHelper mydb1;
    private ListView obj;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mydb1 = new DBHelper(this);
        setContentView(R.layout.email_display);
        ArrayList array_list = mydb1.getAllCotacts();
        ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, array_list);

        obj = (ListView) findViewById(R.id.listView2);
        obj.setAdapter(arrayAdapter);


            send  =(Button) findViewById(R.id.send_button);

            send.setOnClickListener(new View.OnClickListener()

                                    {
                                        @Override
                                        public void onClick(View v) {
                                            try {
                                                Intent emailIntent = new Intent(Intent.ACTION_SEND);
                                                emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"myemail@gmail.com"});
                                                emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
                                                emailIntent.putExtra(Intent.EXTRA_TEXT, "HEY");
                                                emailIntent.putExtra(Intent.EXTRA_CC, "example@gmail.com");
                                                emailIntent.setType("message/rfc822");
                                                startActivity(emailIntent);
                                            } catch (ActivityNotFoundException anfe) {
                                                Toast toast = Toast.makeText(email.this, "Sorry, no email client found", Toast.LENGTH_LONG);
                                                toast.show();
                                            }
                                        }
                                    }

            );
        }
    }
  • Are there any apps on your test device that can receive an intent of MIME type `message/rfc822`? – Daniel May 18 '16 at 11:57
  • 1
    Could you give more information about the error please? MimeType seems to be the problem, http://www.tutorialspoint.com/android/android_sending_email.htm make a look at it – M. Mariscal May 18 '16 at 11:57
  • [See this question](http://stackoverflow.com/questions/8701634/send-email-intent) – ZeusNet May 18 '16 at 12:00

2 Answers2

0

On your button click :

Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"youremail@yahoo.com"});          
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "subject");
emailIntent.putExtra(Intent.EXTRA_TEXT, "message");
emailIntent.setType("message/rfc822");
startActivity(Intent.createChooser(emailIntent, "Choose an Email client :"));

As per your code change

startActivity(emailIntent);

to

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

OR

try with ,

Instead use a ACTION_SENDTO, providing the mailto: Uri

  intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:"));

This may helps you.

Sathish Kumar J
  • 4,280
  • 1
  • 20
  • 48
0
    Intent i = new Intent(Intent.ACTION_SEND);
    i.setType("text/plain");
    i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"abc@gmail.com"});
    i.putExtra(Intent.EXTRA_SUBJECT, "subject");
    i.putExtra(Intent.EXTRA_TEXT   , "body");
    i.putExtra(Intent.EXTRA_CC, new String[] { "edf@gmail.com" });
    try {
        startActivity(Intent.createChooser(i, "Send mail..."));
    } catch (android.content.ActivityNotFoundException ex) {
        Toast.makeText(MyActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
    }

Edit

Try using:

 i.setType("text/plain");
Android Geek
  • 8,956
  • 2
  • 21
  • 35