【问题标题】:How to send email to multiple recepients in android?如何在android中向多个收件人发送电子邮件?
【发布时间】:2013-01-24 08:17:35
【问题描述】:

我是安卓新手。请帮我。我无法向多个收件人发送电子邮件。 这是我的代码。

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();
        }
    });
}

}

【问题讨论】:

  • 有什么问题?应用程序会崩溃吗?有什么错误吗?
  • 检查堆栈溢出的这个链接可能对你有帮助stackoverflow.com/questions/9716924/…
  • @Shreya S : 没有错误.. 它根本不发送电子邮件.. 只需打开电子邮件。但我根本无法发送电子邮件
  • 从 List 到 string[] 的转换错误,即 List list = new ArrayList(); String[] arrayOfStrings = list.toArray(new String[list.size()]);实际上你需要这样做。
  • 在 Intent 之前可以添加 android.content.Intent.ACTION_SEND android.content.Intent.EXTRA_EMAIL android.content.Intent.EXTRA_SUBJECT android.content.Intent.EXTRA_TEXT 你可以这样做试试... :)

标签: android email


【解决方案1】:

首先您从 List 到 String[] 的转换是错误的,您需要执行以下操作..

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

接下来你需要提到 android.Content.Intent 如下..

所以最后你需要改变如下

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 :"));

【讨论】:

  • 非常感谢您的帮助:)
【解决方案2】:

不要使用

public Intent putExtra (String name, String value)

在设置电子邮件收件人时,还有另一种方法可以接受必须用于电子邮件的字符串数组

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

所以你的块

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_EMAIL, to);

有关使用 Intents 的更多详细信息,请参阅 Android 开发人员参考资料,特别是 EXTRA_EMAIL 参数,它需要一个字符串数组,而不是单个字符串。

【讨论】:

  • 尝试使用 public Intent putStringArrayListExtra (String name, ArrayList&lt;String&gt; value) 并将 emailList 作为值传递 - 我只是注意到 to 被声明为 Object[] 而不是 String[] 由于 toArray 方法的返回值可能会导致问题
  • 我的一半问题现在解决了。只是按照 Harish 的建议。地址出现在电子邮件客户端的收件人字段中,但即使显示正在发送消息,也不会发送电子邮件
  • 这将是我的下一个建议,我认为 string array list extra 方法只是同一件事的一种方便方法,很高兴听到你把它排序:)
【解决方案3】:

如果我是你,我会把它放在不同的线程上,这样你的 Activity 线程(或 UI 线程)上就没有任何进程。这是一个很好的android tutorial 如何做到这一点。在 Android 中理解线程非常重要。如果你有时间我也会看这个threading tutorial

button.Onclick(){
  // get all the messages information
  // the button to send the emails has been collected
  new SendEmailTask().execute(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
  }

}

祝你好运!

【讨论】:

    猜你喜欢
    • 2019-03-01
    • 2012-05-18
    • 2019-04-22
    • 2013-11-10
    • 2015-01-08
    • 1970-01-01
    • 2017-12-05
    相关资源
    最近更新 更多