英文:
How to send data(table) from database via email in java?
问题
I can send regular emails (not from the database), but how could I send a full table in an email? This is my code:
private static Message preparedMessage(Session session, String myAccountEmail, String recipient) {
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(myAccountEmail));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipient));
message.setSubject("Email using Java");
String htmlTest = "<h2>i'm sending an email!</h2>";
message.setContent(htmlTest, "text/html");
return message;
} catch (Exception ex) {
Logger.getLogger(JavaMailUtil.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
I tried something simple like this:
List<PetrolData> petrolList = MP_RacunDAO.select("03.08.2020", "11.08.2020"); // This is data that I want to put in a table
for (PetrolData pd : petrolList) {
String htmlTable = "<table border='1'>"
+ "<tr>"
+ "<th>Column 1</th>"
+ "<th>Column 2</th>"
+ "<th>Column 3</th>"
+ "</tr>"
+ "<tr>"
+ "<td>" + pd.getTitle() + "</td>"
+ "<td>" + pd.getSum1() + "</td>"
+ "<td>" + pd.getSum2() + "</td>"
+ "</tr>"
+ "</table>";
}
Obviously, it is not working for me. What can I do?
英文:
I can send regular emails (not from database), but how could I send full table in an email?
This is my code:
private static Message preparedMessage(Session session, String myAccountEmail,
String recepient) {
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(myAccountEmail));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(recepient));
message.setSubject("Email using Java");
String htmlTest="<h2> i'm sending an email!</h2>";
message.setContent(htmlTest, "text/html");
}
return message;
} catch (Exception ex) {
Logger.getLogger(JavaMailUtil.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
I tried something simple like this:
List <PetrolData> petrolList = MP_RacunDAO.select("03.08.2020", "11.08.2020"); //This is data that i want to put in table
for(PetrolData pd : petrolList){
String htmlTable = "<table border='1'> <tr> "
+ "<th>Column 1</td>"
+ "<th>Column 2</td>"
+ "<th>Column 3</td>"
+ "</tr>"
+ "<tr>"
+ "<td>"+pd.getTitle()+"</td>"
+ "<td>"+pd.getSum1()+"</td>"
+ "<td>"+pd.getSum2()+"</td>"
+ "</tr>"
+ "</table>";
}
Obviously, It is not working for me, what can I do?
答案1
得分: 0
如果您想在不使用任何外部库的情况下在一行中创建一个HTML表格,您可以使用StringBuilder
来实现。
您基本上需要在循环遍历数据之前添加表格的开放标签和标题行,然后为每个PetrolData
添加一行,并在循环之后添加表格的闭合标签。
看一下这个例子(其中使用了潜在的不必要的换行和制表符,但它们对于示例输出很有用,因为我不能在这里发送电子邮件):
public static void main(String[] args) {
// 示例数据
List<PetrolData> petrolDataList = new ArrayList<>();
petrolDataList.add(new PetrolData("Petrol 1", 4.1f, 8.2f, 12.4f));
petrolDataList.add(new PetrolData("Petrol 2", 5.3f, 9.2f, 13.24f));
petrolDataList.add(new PetrolData("Petrol 3", 6.0f, 10.1f, 14.312f));
petrolDataList.add(new PetrolData("Petrol 4", 7.7f, 11.9f, 15.15f));
petrolDataList.add(new PetrolData("Petrol 5", 8.45f, 12.0f, 16.936f));
petrolDataList.add(new PetrolData("Petrol 6", 9.67f, 13.2f, 17.777f));
StringBuilder htmlTableBuilder = new StringBuilder();
// 打开表格
htmlTableBuilder.append("<table border=\"1\">").append(System.lineSeparator());
// 打开一行,这里是标题行
htmlTableBuilder.append("\t").append("<tr>").append(System.lineSeparator());
// 将标题添加到标题行
htmlTableBuilder.append("\t").append("\t").append("<th>").append("Title").append("</th>").append(System.lineSeparator());
htmlTableBuilder.append("\t").append("\t").append("<th>").append("Sum 1").append("</th>").append(System.lineSeparator());
htmlTableBuilder.append("\t").append("\t").append("<th>").append("Sum 2").append("</th>").append(System.lineSeparator());
htmlTableBuilder.append("\t").append("\t").append("<th>").append("Sum 3").append("</th>").append(System.lineSeparator());
// 关闭标题行
htmlTableBuilder.append("\t").append("</tr>").append(System.lineSeparator());
// 为每个PetrolData添加一行数据
for (PetrolData pd : petrolDataList) {
// 再次打开一行
htmlTableBuilder.append("\t").append("<tr>").append(System.lineSeparator());
// 添加数据
htmlTableBuilder.append("\t").append("\t").append("<td>").append(pd.getTitle()).append("</td>").append(System.lineSeparator());
htmlTableBuilder.append("\t").append("\t").append("<td>").append(pd.getSum1()).append("</td>").append(System.lineSeparator());
htmlTableBuilder.append("\t").append("\t").append("<td>").append(pd.getSum2()).append("</td>").append(System.lineSeparator());
htmlTableBuilder.append("\t").append("\t").append("<td>").append(pd.getSum3()).append("</td>").append(System.lineSeparator());
// 关闭行
htmlTableBuilder.append("\t").append("</tr>").append(System.lineSeparator());
}
// 并关闭表格
htmlTableBuilder.append("</table>");
// 然后打印结果
System.out.println(htmlTableBuilder.toString());
}
这个示例的输出是以下内容,您可以轻松地嵌入到有效的HTML邮件中:
<table border="1">
<tr>
<th>Title</th>
<th>Sum 1</th>
<th>Sum 2</th>
<th>Sum 3</th>
</tr>
<tr>
<td>Petrol 1</td>
<td>4.099999904632568</td>
<td>8.199999809265137</td>
<td>12.399999618530273</td>
</tr>
<tr>
<td>Petrol 2</td>
<td>5.300000190734863</td>
<td>9.199999809265137</td>
<td>13.239999771118164</td>
</tr>
<tr>
<td>Petrol 3</td>
<td>6.0</td>
<td>10.100000381469727</td>
<td>14.312000274658203</td>
</tr>
<tr>
<td>Petrol 4</td>
<td>7.699999809265137</td>
<td>11.899999618530273</td>
<td>15.149999618530273</td>
</tr>
<tr>
<td>Petrol 5</td>
<td>8.449999809265137</td>
<td>12.0</td>
<td>16.93600082397461</td>
</tr>
<tr>
<td>Petrol 6</td>
<td>9.670000076293945</td>
<td>13.199999809265137</td>
<td>17.777000427246094</td>
</tr>
</table>
请注意,double
的格式,您可能需要应用DecimalFormat
或类似的内容来显示正确的值。
英文:
If you want to create an HMTL table in a row without using any external library, you can use a StringBuilder
for that.
You would basically have to add the opening tag of the table and the header row before you loop through the data, then add a row for each PetrolData
and after the loop you add the closing tag of the table.
See this example (which uses potentially unnecessary linebreaks and tabs, but they are useful for example output, because I won't send an email here):
public static void main(String[] args) {
// sample data
List<PetrolData> petrolDataList = new ArrayList<>();
petrolDataList.add(new PetrolData("Petrol 1", 4.1f, 8.2f, 12.4f));
petrolDataList.add(new PetrolData("Petrol 2", 5.3f, 9.2f, 13.24f));
petrolDataList.add(new PetrolData("Petrol 3", 6.0f, 10.1f, 14.312f));
petrolDataList.add(new PetrolData("Petrol 4", 7.7f, 11.9f, 15.15f));
petrolDataList.add(new PetrolData("Petrol 5", 8.45f, 12.0f, 16.936f));
petrolDataList.add(new PetrolData("Petrol 6", 9.67f, 13.2f, 17.777f));
StringBuilder htmlTableBuilder = new StringBuilder();
// open the table
htmlTableBuilder.append("<table border=\"1\">").append(System.lineSeparator());
// open a rown, header row here
htmlTableBuilder.append("\t").append("<tr>").append(System.lineSeparator());
// add the headers to the header row
htmlTableBuilder.append("\t").append("\t").append("<th>").append("Title").append("</th>").append(System.lineSeparator());
htmlTableBuilder.append("\t").append("\t").append("<th>").append("Sum 1").append("</th>").append(System.lineSeparator());
htmlTableBuilder.append("\t").append("\t").append("<th>").append("Sum 2").append("</th>").append(System.lineSeparator());
htmlTableBuilder.append("\t").append("\t").append("<th>").append("Sum 3").append("</th>").append(System.lineSeparator());
// close the header row
htmlTableBuilder.append("\t").append("</tr>").append(System.lineSeparator());
// add a data row for each PetrolData
for (PetrolData pd : petrolDataList) {
// open a row again
htmlTableBuilder.append("\t").append("<tr>").append(System.lineSeparator());
// add the data
htmlTableBuilder.append("\t").append("\t").append("<td>").append(pd.getTitle()).append("</td>").append(System.lineSeparator());
htmlTableBuilder.append("\t").append("\t").append("<td>").append(pd.getSum1()).append("</td>").append(System.lineSeparator());
htmlTableBuilder.append("\t").append("\t").append("<td>").append(pd.getSum2()).append("</td>").append(System.lineSeparator());
htmlTableBuilder.append("\t").append("\t").append("<td>").append(pd.getSum3()).append("</td>").append(System.lineSeparator());
// close the row
htmlTableBuilder.append("\t").append("</tr>").append(System.lineSeparator());
}
// and close the table
htmlTableBuilder.append("</table>");
// then print the result
System.out.println(htmlTableBuilder.toString());
}
The output of this is the following, which you could easily embed in a valid HTML mail:
<table border="1">
<tr>
<th>Title</th>
<th>Sum 1</th>
<th>Sum 2</th>
<th>Sum 3</th>
</tr>
<tr>
<td>Petrol 1</td>
<td>4.099999904632568</td>
<td>8.199999809265137</td>
<td>12.399999618530273</td>
</tr>
<tr>
<td>Petrol 2</td>
<td>5.300000190734863</td>
<td>9.199999809265137</td>
<td>13.239999771118164</td>
</tr>
<tr>
<td>Petrol 3</td>
<td>6.0</td>
<td>10.100000381469727</td>
<td>14.312000274658203</td>
</tr>
<tr>
<td>Petrol 4</td>
<td>7.699999809265137</td>
<td>11.899999618530273</td>
<td>15.149999618530273</td>
</tr>
<tr>
<td>Petrol 5</td>
<td>8.449999809265137</td>
<td>12.0</td>
<td>16.93600082397461</td>
</tr>
<tr>
<td>Petrol 6</td>
<td>9.670000076293945</td>
<td>13.199999809265137</td>
<td>17.777000427246094</td>
</tr>
</table>
Pay attention to the format of double
s, you might have to apply a DecimalFormat
or anything similar for correct values to be shown.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论