问题描述
我是Android的新手.请帮我.我无法向多个收件人发送电子邮件. 这是我的代码.
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(); } }); } }
推荐答案
首先将您的转换从列表到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 :"));
其他推荐答案
请勿使用
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 特别是 aftor_email 参数,它期望字符串数组,而不是单个字符串.
其他推荐答案
如果我是你,我会把它放在一个不同的线程中,以便在活动线程(或ui线程)上没有任何进程.这是一个很好的 Android教程如何执行此操作.在Android中了解线程非常重要.如果你有时间,我会看这个手段教程.
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 } }
祝你好运!
问题描述
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(); } }); } }
推荐答案
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 :"));
其他推荐答案
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.
其他推荐答案
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!