[英]How to send dynamic html table in email body using java
我有一个具有 getProductwiseCount 方法的 callProcedure 类。
通过 callprocedure 类,我正在调用另一个类的 sendMail 方法。我想从 html 中的 hashmap 的键和值对生成动态表并将其附加到邮件中。 我该怎么做?
public HashMap<Long, String> getProductwiseCount() {
return ProductwiseCount;
}
I did it by pre-generating html in callProcedure class. Save it in to String and passed it in a sendEmail method.
String text=
"<table width='100%' border='1' align='center'>"
+ "<tr align='center'>"
+ "<td><b>Product Name <b></td>"
+ "<td><b>Count<b></td>"
+ "</tr>";
for (Map.Entry entry : ProductwiseCount.entrySet()) {
System.out.println(entry.getKey() + " :" + entry.getValue());
text=text+"<tr align='center'>"+"<td>" + entry.getValue() + "</td>"
+ "<td>" + entry.getKey() + "</td>"+"</tr>";
}
sendMail.sendMail(host, port, to, from,text);
将 html 添加到电子邮件正文中很容易。 只需创建要添加到正文中的 HTML 并设置为消息正文部分,请检查:
https://www.mkyong.com/java/javamail-api-sending-email-via-gmail-smtp-example/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.