2013-01-21 15 views
0

我已經開發了一個發送電子郵件android應用程序。action_send工作... javamailapi不工作在android sendmail函數

在這裏,我必須發送多個產品的詳細信息發送到電子郵件。

我用下面的代碼:

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("[email protected]","arirajaguru"); 
        } 
       }); 

      try { 

       Message message = new MimeMessage(session); 
       message.setFrom(new InternetAddress("[email protected]")); 
       message.setRecipients(Message.RecipientType.TO, 
         InternetAddress.parse("[email protected]")); 
       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[] { "[email protected],[email protected]" }); 

       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郵件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); 

什麼是錯在我code.please幫助我。

回答

0

我在使用stringbuffer並追加後得到了輸出。

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