Android-如何将字符串数组作为电子邮件正文发送

时间:2015-01-21 10:38:08

标签: android email

我需要在按钮点击时发送一些数据作为电子邮件。我成功添加了编辑文本中的数据。但我不知道如何从我的数据库发送数据数据。在我的代码中我需要发送pn [i的值我添加了这样的值。但它在这一行引起了错误

emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,"name:"+edt1.getText().toString()+"\n"+"products:"+pn[i]);

代码:

    btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            mydb=buy_ltr.this.openOrCreateDatabase("addcart", MODE_PRIVATE, null);
            Cursor cr = mydb.rawQuery("SELECT * FROM add2cart WHERE usr='"+cont+"'", null);
            String [] pn = new String[cr.getCount()];

            int i = 0;
            while(cr.moveToNext())
            {

                String prp = cr.getString(cr.getColumnIndex("prate"));
                pn[i] = prp;
                i++;
            }


              Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
              String[] recipients = new String[]{"my@email.com", "",};
              emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, recipients);
              emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Test");
              emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "This is email's message");
              emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,"name:"+edt1.getText().toString()+"\n"+"address:"+edt2.getText().toString()+"\n"+"products:"+pn[i]);
              emailIntent.setType("text/plain");
    //        emailIntent.setType("message/rfc822");
              startActivity(Intent.createChooser(emailIntent, "Send mail..."));
              finish();

        }
    });

1 个答案:

答案 0 :(得分:2)

方法一:(内置方法)

String productNames = Arrays.toString(pn);

方法二:(新产品上的每个产品)

StringBuilder productNamesBuilder = new StringBuilder();
for(String productName: pn){
    productNamesBuilder.append(productName + "\n");
}
String productNames = productNamesBuilder.toString();

方法三:(编号列表,新行上的每个产品)

StringBuilder productNamesBuilder = new StringBuilder();
for(int i = 0; i < pn.length; i++){
    productNamesBuilder.append((i + 1) + " " + pn[i] + "\n");
}
String productNames = productNamesBuilder.toString();