• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps send more than one items to mail from android application

krishnaveni

Well-Known Member
I have to develop one android example.

More than one item detail is send to mail from my android application.

Here i have to run the app means the latest added product detail alone send to mail.but i have to send the all added product detail is send to email.How can i do.please help me.whats wrong in my code.please check the code and give me solution for these.

I have used below code:
HTML:
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);
        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","xxxxxx");
                        }
                    });

                try {

                    Message message = new MimeMessage(session);
                    message.setFrom(new InternetAddress("krishnaveniv96@gmail.com"));
                    message.setRecipients(Message.RecipientType.TO,
                            InternetAddress.parse("mercy.krishnaveni@gmail.com"));
                    message.setSubject("Testing Subject");
                    StringBuilder sb = new StringBuilder();                  
                  //  message.setContent("This is your product name : "+
                        //    "Hi Krishna" +"<br></br>This is your price : "+ "Hi veni", "text/html; charset=utf-8");
                    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);

                    message.setContent("<tr>" + "<td>" + title
                                + "</td><td>" + qty + " * " + cost
                                + "</td>" + " = <td>" + total
                                + "  " + "</td></tr>", "text/html; charset=utf-8"); 
                    
                    }
                    
                    Transport.send(message);

                    System.out.println("Done");

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



      });
 
Hi
i got the o/p..The more than one product detail is send to email successfully done.

These is code :
Multipart multipart = new MimeMultipart();
BodyPart messageBodyPart = new MimeBodyPart();
StringBuffer sb = new StringBuffer();
sb.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);

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

}

StringBuffer buffer2 = sb.append("<tr>" + "<td>" + "Grand Total is:- "
+ "</td><td>" + mGrandTotal + " "
+ "</td></tr>");
BodyPart totalpart = new MimeBodyPart();

totalpart.setDataHandler(new DataHandler( new ByteArrayDataSource(buffer2.toString(), "text/html; charset=utf-8")));
sb.append("</table></body></html>");
multipart.addBodyPart(messageBodyPart);
multipart.addBodyPart(totalpart);
message.setContent(multipart);

Transport.send(message);

But the product detail is sent twice in single email...

like below:

Rialto: Expose - Natural Cork1 * 31.6 = 31.6 Laura Ashley Kids: LA20016 (Infant/Toddler/Youth)1 * 18.06 = 18.06
Rialto: Expose - Natural Cork1 * 31.6 = 31.6 Laura Ashley Kids: LA20016 (Infant/Toddler/Youth)1 * 18.06 = 18.06 Grand Total is:- 49.66
How can i send the product detail is once in single email.whats wrong in my code.please help me.
 
Back
Top Bottom