如何在Android中发送电子邮件到多个收件人? [英] How to send email to multiple recepients in android?
本文介绍了如何在Android中发送电子邮件到多个收件人?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的代码。
public class SendEmailActivity extends Activity {
EditText subject_ed, message_ed;
TextView subject_tv,message_tv;
按钮send_btn;
ArrayList< String>的emailList;
ArrayList< Integer> IDLIST;
int eventId;
@Override
protected void onCreate(Bundle savedInstanceState){
// TODO自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(R.layout.contacts_email_sms_layout);
setupViews();
Intent intent = getIntent();
Bundle b = intent.getExtras();
eventId = b.getInt(EventId); //事件id
idList = b.getIntegerArrayList(IdList); // Ids列表
emailList = b.getStringArrayList( EmailList); //电子邮件ID列表
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自动生成的方法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);
(int i = 0; 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);
//需要这个来提示电子邮件客户端
email.setType(message / rfc822);
startActivity(Intent.createChooser(email,Choose a Email client:));
// finish();
}
});
}
}
解决方案
首先,从List到String []的转换是错误的,您需要执行如下操作。
列表< 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); (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); //或email.setType(text / plain);
startActivity(Intent.createChooser(email,Choose a Email client:));
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 :"));
这篇关于如何在Android中发送电子邮件到多个收件人?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文