How to send email to multiple recepients in android?

后端 未结 3 1206
难免孤独
难免孤独 2020-12-10 23:27

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 Activit         


        
相关标签:
3条回答
  • 2020-12-11 00:02

    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 :"));
    
    0 讨论(0)
  • 2020-12-11 00:03

    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.

    0 讨论(0)
  • 2020-12-11 00:03

    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!

    0 讨论(0)
提交回复
热议问题