java 如何使用java在电子邮件正文中发送动态html表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40108157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to send dynamic html table in email body using java
提问by new coder
I have a class callProcedure which has method getProductwiseCount.
我有一个具有 getProductwiseCount 方法的 callProcedure 类。
Through callprocedure class I am calling sendMail method of another class.I want to generate dynamic table from key and value pairs from hashmap in html and attached it in mail. How do I do it?
通过 callprocedure 类,我正在调用另一个类的 sendMail 方法。我想从 html 中的 hashmap 的键和值对生成动态表并将其附加到邮件中。我该怎么做?
public HashMap<Long, String> getProductwiseCount() {
return ProductwiseCount;
}
回答by Jay Prakash
Its to easy to add html into email body. Just create HTML you want to add into body and set to message body part,Check this:
将 html 添加到电子邮件正文中很容易。只需创建要添加到正文中的 HTML 并设置为消息正文部分,请检查:
https://www.mkyong.com/java/javamail-api-sending-email-via-gmail-smtp-example/
https://www.mkyong.com/java/javamail-api-sending-email-via-gmail-smtp-example/
回答by new coder
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);