1

I'm a newbie in android. Please help me. I'm not able to send email to multiple recipients. Here is my code.

public class SendEmailActivity extends Activity{

EditText subject_ed,message_ed;
TextView subject_tv,message_tv;
Button send_btn;

 ArrayList<String> emailList;
 ArrayList<Integer> idList;
 int eventId;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.contacts_email_sms_layout);
    setupViews();
    Intent intent = getIntent();
    Bundle b = intent.getExtras();
    eventId =  b.getInt("EventId");//event id
    idList = b.getIntegerArrayList("IdList");//list of Ids
    emailList = b.getStringArrayList("EmailList");//list of email ids
    buttonListeners();
}

public void setupViews()
{
    subject_ed = (EditText)findViewById(R.id.ed_subject_email);
    message_ed = (EditText)findViewById(R.id.ed_msg_body);
    subject_tv = (TextView)findViewById(R.id.tv_subject_email);
    message_tv = (TextView)findViewById(R.id.tv_msg_body);
    send_btn = (Button)findViewById(R.id.btn_send_sms_email);
}               

public void buttonListeners()
{
    send_btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), "Email sent",Toast.LENGTH_LONG).show();
            // String to = textTo.getText().toString();
              String subject = subject_ed.getText().toString();
              String message = message_ed.getText().toString();
            Object[] to =  emailList.toArray();
//            for(int i = 0; i<=emailList.size(); i++)
////                  {
////                      
//                  String  to=   emailList.get(0);
////                     
////                  }



              Intent email = new Intent(Intent.ACTION_SEND);
              for(int i = 0; i < to.length; i++)
                {
                    Log.i("String is", (String)to[i]);
                    //String[] str = (String[])to[i];
                     email.putExtra(Intent.EXTRA_EMAIL,",'" +(String)to[i] + "'");
                }

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

}
Abhinav Singh Maurya
  • 3,313
  • 8
  • 33
  • 51
Deepthi
  • 875
  • 1
  • 12
  • 20
  • What is the problem? Does the app crash? Any errors? – MysticMagicϡ Jan 24 '13 at 08:24
  • check this link of stack overflow may be this will help you http://stackoverflow.com/questions/9716924/send-email-to-multiple-addresses-android – Abhinav Singh Maurya Jan 24 '13 at 08:27
  • @Shreya S : No errors.. It doesnt sent email at all.. Just bring up the email. but I cant sent email at all – Deepthi Jan 24 '13 at 08:29
  • @Abhinav Singh Maurya: Thanks for the link but I tried this. not working for multiple recepients!! – Deepthi Jan 24 '13 at 08:30
  • Can't u pass more than one string in that string array? – MysticMagicϡ Jan 24 '13 at 08:31
  • @Shreya: I'm able to get the email ids in the LogCat. But the same thing is not appearing in the 'to' field. – Deepthi Jan 24 '13 at 08:33
  • you are putting to much extras for to field may be problem is due to that. try to add array directly and then check if its working or not – Abhinav Singh Maurya Jan 24 '13 at 08:38
  • @ Abhinav Singh Maurya: I'm able to click the sent button and it will go to the email client in the device. But subject and message part will be filled up as my edittext values but the to field is not filled up.. and if i give email ids manually and press send button, the sending process shows as sending and closes down but the email is not sent at all – Deepthi Jan 24 '13 at 08:41
  • @Abhinav Singh Maurya:its not working still – Deepthi Jan 24 '13 at 08:46
  • 1
    Conversion wrong from List to string[] that is List list = new ArrayList(); String[] arrayOfStrings = list.toArray(new String[list.size()]); actually you need to do like this. – Harish Jan 24 '13 at 08:54
  • @Harish : Thanks a lot my half problem is solved now.. The addresses are appearing in the to field of the email client but emails are not sent even though it shows sending message.. – Deepthi Jan 24 '13 at 09:06
  • Once try by changing intentname.setType("text/plain"); – Harish Jan 24 '13 at 09:13
  • @Harish : Still doesnt work :( – Deepthi Jan 24 '13 at 09:17
  • can you please keep the startActivity(Intent.createChooser(email, "Send mail...")); this in try block and catch the error so that we know getting any exception. – Harish Jan 24 '13 at 09:23
  • @Harish : I'm not getting any exception – Deepthi Jan 24 '13 at 09:31
  • have you written anything in your catch block like toast or log message.. – Harish Jan 24 '13 at 09:33
  • yup log.. but nothing is showing in the logcat – Deepthi Jan 24 '13 at 09:34
  • 1
    Before Intent can you add android.content.Intent.ACTION_SEND android.content.Intent.EXTRA_EMAIL android.content.Intent.EXTRA_SUBJECT android.content.Intent.EXTRA_TEXT you can do like this try...:) – Harish Jan 24 '13 at 09:40
  • @ Harish : I'll try this too.. :( – Deepthi Jan 24 '13 at 09:51
  • have you solved your problem – Harish Jan 24 '13 at 12:02

3 Answers3

5

First your conversion from List to String[] is wrong you need to do as follows..

List<String> list = new ArrayList<String>();
String[] arrayOfStrings = list.toArray(new String[list.size()]);

And next thing is you need to mention android.Content.Intent as follows..

So finally you need to change as follows

ArrayList<String> emailList;
emailList = b.getStringArrayList("EmailList");
String[] emailArray;

Intent email = new Intent(android.content.Intent.ACTION_SEND);

for(int i = 0; i < to.length; i++){
    Log.i("String is", (String)to[i]);
    email.putExtra(android.content.Intent.EXTRA_EMAIL, 
                   emailList.toArray(new String[emailList.size()]));
}
email.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
email.putExtra(android.content.Intent.EXTRA_TEXT, message);    
email.setType("message/rfc822"); //or email.setType("text/plain");

startActivity(Intent.createChooser(email, "Choose an Email client :"));
Junior Mayhé
  • 16,144
  • 26
  • 115
  • 161
Harish
  • 3,122
  • 2
  • 31
  • 46
1

Do not use

public Intent putExtra (String name, String value)

When setting email recipients, instead there is another method which accepts a string array which must be used for emails

public Intent putExtra (String name, String[] value)

So your block

for(int i = 0; i < to.length; i++)
{
    Log.i("String is", (String)to[i]);
    //String[] str = (String[])to[i];
    email.putExtra(Intent.EXTRA_EMAIL,",'" +(String)to[i] + "'");
}

Would simply become

email.putExtra(Intent.EXTRA_EMAIL, to);

See the android developer reference for more details on using Intents specifically the EXTRA_EMAIL argument which expects a string array, not a single string.

rich200313
  • 56
  • 3
  • Try using `public Intent putStringArrayListExtra (String name, ArrayList value)` and passing in emailList as value - I just noticed that to is declared as Object[] rather than String[] due to the return value of the toArray method which may cause an issue – rich200313 Jan 24 '13 at 08:55
  • My half problem is solved now..Just followed What Harish suggested. The addresses are appearing in the to field of the email client but emails are not sent even though it shows sending message – Deepthi Jan 24 '13 at 09:22
  • That was going to be my next suggestion, I thought the string array list extra method would simply be a convenience method for the same thing, glad to hear you got it sorted :) – rich200313 Jan 24 '13 at 09:35
0

If I were you I would put this on a different thread so that you don't have any process on the Activity thread (or UI thread). This is a good android tutorial on how to do this. Threading is really important to understand in Android. If you have time I would watch this threading tutorial as well.

button.Onclick(){
  // get all the messages information
  // the button to send the emails has been collected
  new SendEmailTask().execute(messages)
}

Then in your Async Task you can send all of the messages

SendEmailTask extends AsyncTask<Message,Void,Void>(){

  function doInBackground(Message... msgs){
    for(Message m : msgs){
     // process each of your messages
     // send the messages out
    }
  }
  function onPostExecute(){
    // tell the UI thread that you are finished
  }

}

Good Luck!

jpotts18
  • 4,951
  • 5
  • 31
  • 31