我开发了一个发送电子邮件的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);
我的代码中有什么问题。请帮助我。
答案 0 :(得分:0)
我在使用stringbuffer后得到了输出并追加。
StringBuffer sb = new StringBuffer();
sb.append("<html><body><table>");
in 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")));