0

I am New to Android.I am trying to send mail to all the selected recipients(getting from JSON), selecting the recipients through checkbox.The dialog box for sending email appears again and again for each but I want to send in one time.I am posting my code.

ArrayList<Getter_Setter> user_list;
btn_send.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            for (int i = 0; i < user_list.size(); i++) {
                //send mail with these selected emails
                if (user_list.get(i).isSelected()) {
                    String[] item = {user_list.get(i).getEmail()};

                    Intent intent = new Intent(Intent.ACTION_SEND);
                    intent.setType("text/plain");
                    intent.putExtra(Intent.EXTRA_EMAIL, new String [item.length]);
                    intent.putExtra(Intent.EXTRA_SUBJECT, "");
                    intent.putExtra(Intent.EXTRA_TEXT, "");
                    try {
                        startActivity(Intent.createChooser(intent, "Send mail..."));
                    } catch (android.content.ActivityNotFoundException ex) {
                        Toast.makeText(UserInfo.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        }
    });

2 Answers2

1

U r doing the part for sending the email in a for loop. Hence the mail dialog appears many times.

Put the code for sending mail after the loop.Like this.

String[] item = new String[user_list.size()];
int index = 0;

        for (int i = 0; i < user_list.size(); i++) {
            //send mail with these selected emails
            if (user_list.get(i).isSelected()) {
                item[index] = user_list.get(i).getEmail();
                index++;
            }
        }

Also in the Intent.EXTRA_EMAIL, u need to pass the array with the actual email id's.

                Intent intent = new Intent(Intent.ACTION_SEND);
                intent.setType("text/plain");
                intent.putExtra(Intent.EXTRA_EMAIL, item);
                intent.putExtra(Intent.EXTRA_SUBJECT, "");
                intent.putExtra(Intent.EXTRA_TEXT, "");
                try {
                    startActivity(Intent.createChooser(intent, "Send mail..."));
                } catch (android.content.ActivityNotFoundException ex) {
                    Toast.makeText(UserInfo.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
                }
vishnus
  • 728
  • 2
  • 7
  • 20
0

If String[] item = {user_list.get(i).getEmail()}; is a String[] of email addresses then change your EXTRA_EMAIL from

intent.putExtra(Intent.EXTRA_EMAIL, new String [item.length]);

to

intent.putExtra(Intent.EXTRA_EMAIL, item);
lodlock
  • 3,638
  • 1
  • 19
  • 15
  • putting "item" doesn't work because it shows the dialog again and again for multiple selects. –  Dec 17 '15 at 05:03