0

我开发了一个发送电子邮件的 android 应用程序。

在这里,我必须发送多个产品详细信息发送到电子邮件。

我使用了以下代码:

  public class InvoiceOrder extends Activity {


        String mGrandTotal;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub  
    super.onCreate(savedInstanceState);
    setContentView(R.layout.invoice);
    Bundle b = getIntent().getExtras();
    //String s= getIntent().getStringExtra("orderid");
    mGrandTotal = b.getString("GrandTotal");

    ListView mLstView1 = (ListView) findViewById(R.id.listView1);


    CustomerAdapter mViewCartAdpt = new CustomerAdapter(
            InvoiceOrder.this);
    mLstView1.setAdapter(mViewCartAdpt);

            Button login = (Button) findViewById(R.id.mBtnSubmit);
            login.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {
            Properties props = new Properties();
            props.put("mail.smtp.host", "smtp.gmail.com");
            props.put("mail.smtp.socketFactory.port", "465");
            props.put("mail.smtp.socketFactory.class",
                    "javax.net.ssl.SSLSocketFactory");
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.port", "465");

            Session session = Session.getDefaultInstance(props,
                new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication("krishnaveniv96@gmail.com","arirajaguru");
                    }
                });

            try {

                Message message = new MimeMessage(session);
                message.setFrom(new InternetAddress("krishnaveni.veeman@mercuryminds.com"));
                message.setRecipients(Message.RecipientType.TO,
                        InternetAddress.parse("mercy.krishnaveni@gmail.com"));
                message.setSubject("Testing Subject");
                message.setSentDate(new Date());

                StringBuilder body = new StringBuilder();
                body.append("<html><body><table>"); 
                for (int i = 0; i < Constants.mItem_Detail
                        .size(); i++) {

                    String title = Constants.mItem_Detail
                            .get(i).get(
                                    SingleMenuItem.KEY_PNAME);

                    String qty = Constants.mItem_Detail.get(i)
                            .get(SingleMenuItem.KEY_QTY);

                    String cost = Constants.mItem_Detail.get(i)
                            .get(SingleMenuItem.KEY_PRICE);

                    String total = Constants.mItem_Detail
                            .get(i).get(
                                    SingleMenuItem.KEY_TOTAL);

                    body.append("<tr>" + "<td>" + title
                            + "</td><td>" + qty + " * " + cost
                            + "</td>" + " = <td>" + total
                            + "  " + "</td></tr>");
                }

                body.append("<tr>" + "<td>" + "Grand Total is:-  "
                        + "</td><td>" + mGrandTotal + " "
                    + "</td></tr>");
                body.append("</table></body></html>"); 
    final Intent emailIntent = new Intent(
                        android.content.Intent.ACTION_SEND);
                emailIntent.setType("text/html");
                emailIntent
                        .putExtra(
                                Intent.EXTRA_EMAIL,
                                new String[] { "mercy.krishnaveni@gmail.com,krishnaveni.veeman@mercuryminds.com" });

                emailIntent.putExtra(
                        android.content.Intent.EXTRA_TEXT,
                        Html.fromHtml(body.toString()));
                startActivity(Intent.createChooser(emailIntent,
                        "Email:")); 
                System.out.println("Done");

            } catch (MessagingException e) {
                throw new RuntimeException(e);
            }
           }



  });
        }

}

在这里,使用 android 应用程序中的 ACTION_SEND 成功完成了将多个产品发送到电子邮件的操作。

但是我必须使用 java mail api 将多个产品详细信息发送到电子邮件。

我使用了 javamail api 意味着单个产品详细信息仅发送到邮件...但我必须发送多个产品详细信息。

这是我的代码:

Multipart multipart = new MimeMultipart();
                BodyPart messageBodyPart = new MimeBodyPart();
                for (int i = 0; i < Constants.mItem_Detail
                        .size(); i++) {

                    String title = Constants.mItem_Detail
                            .get(i).get(
                                    SingleMenuItem.KEY_PNAME);

                    String qty = Constants.mItem_Detail.get(i)
                            .get(SingleMenuItem.KEY_QTY);

                    String cost = Constants.mItem_Detail.get(i)
                            .get(SingleMenuItem.KEY_PRICE);

                    String total = Constants.mItem_Detail
                            .get(i).get(
                                    SingleMenuItem.KEY_TOTAL);
                    messageBodyPart.setText(title + qty + cost + total);

                }
            multipart.addBodyPart(messageBodyPart);

            message.setContent(multipart);

                 Transport.send(message);

我的代码有什么问题。请帮助我。

4

1 回答 1

0

我在使用 stringbuffer 和 append 后得到了输出。

 StringBuffer sb = new StringBuffer();    
                sb.append("<html><body><table>");

在 for 循环中:

                    StringBuffer buffer = sb.append("<tr>" + "<td>" + title
                            + "</td><td>" + qty + " * " + cost
                            + "</td>" + " = <td>" + total
                            + "  " + "</td></tr>");
                    messageBodyPart.setDataHandler(new DataHandler( new ByteArrayDataSource(buffer.toString(), "text/html; charset=utf-8")));
于 2013-01-21T10:43:57.017 回答