如何在Java中通过电子邮件发送数据库中的数据(表格)?

huangapple go评论99阅读模式
英文:

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(&quot;Email using Java&quot;);
        String htmlTest=&quot;&lt;h2&gt; i&#39;m sending an email!&lt;/h2&gt;&quot;;
        message.setContent(htmlTest, &quot;text/html&quot;); 
       }
        return message;
    } catch (Exception ex) {
        Logger.getLogger(JavaMailUtil.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
}

I tried something simple like this:

     List &lt;PetrolData&gt; petrolList = MP_RacunDAO.select(&quot;03.08.2020&quot;, &quot;11.08.2020&quot;); //This is data that i want to put in table
       for(PetrolData pd : petrolList){
        String htmlTable = &quot;&lt;table border=&#39;1&#39;&gt; &lt;tr&gt; &quot;
                + &quot;&lt;th&gt;Column 1&lt;/td&gt;&quot;
                + &quot;&lt;th&gt;Column 2&lt;/td&gt;&quot;
                + &quot;&lt;th&gt;Column 3&lt;/td&gt;&quot;
                + &quot;&lt;/tr&gt;&quot;
                + &quot;&lt;tr&gt;&quot;
                + &quot;&lt;td&gt;&quot;+pd.getTitle()+&quot;&lt;/td&gt;&quot;
                + &quot;&lt;td&gt;&quot;+pd.getSum1()+&quot;&lt;/td&gt;&quot;
                + &quot;&lt;td&gt;&quot;+pd.getSum2()+&quot;&lt;/td&gt;&quot;                   
                + &quot;&lt;/tr&gt;&quot;
                + &quot;&lt;/table&gt;&quot;;
            }

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&lt;PetrolData&gt; petrolDataList = new ArrayList&lt;&gt;();
	petrolDataList.add(new PetrolData(&quot;Petrol 1&quot;, 4.1f, 8.2f, 12.4f));
	petrolDataList.add(new PetrolData(&quot;Petrol 2&quot;, 5.3f, 9.2f, 13.24f));
	petrolDataList.add(new PetrolData(&quot;Petrol 3&quot;, 6.0f, 10.1f, 14.312f));
	petrolDataList.add(new PetrolData(&quot;Petrol 4&quot;, 7.7f, 11.9f, 15.15f));
	petrolDataList.add(new PetrolData(&quot;Petrol 5&quot;, 8.45f, 12.0f, 16.936f));
	petrolDataList.add(new PetrolData(&quot;Petrol 6&quot;, 9.67f, 13.2f, 17.777f));
	
	StringBuilder htmlTableBuilder = new StringBuilder();
	// open the table
	htmlTableBuilder.append(&quot;&lt;table border=\&quot;1\&quot;&gt;&quot;).append(System.lineSeparator());
	// open a rown, header row here
	htmlTableBuilder.append(&quot;\t&quot;).append(&quot;&lt;tr&gt;&quot;).append(System.lineSeparator());
	// add the headers to the header row
	htmlTableBuilder.append(&quot;\t&quot;).append(&quot;\t&quot;).append(&quot;&lt;th&gt;&quot;).append(&quot;Title&quot;).append(&quot;&lt;/th&gt;&quot;).append(System.lineSeparator());
	htmlTableBuilder.append(&quot;\t&quot;).append(&quot;\t&quot;).append(&quot;&lt;th&gt;&quot;).append(&quot;Sum 1&quot;).append(&quot;&lt;/th&gt;&quot;).append(System.lineSeparator());
	htmlTableBuilder.append(&quot;\t&quot;).append(&quot;\t&quot;).append(&quot;&lt;th&gt;&quot;).append(&quot;Sum 2&quot;).append(&quot;&lt;/th&gt;&quot;).append(System.lineSeparator());
	htmlTableBuilder.append(&quot;\t&quot;).append(&quot;\t&quot;).append(&quot;&lt;th&gt;&quot;).append(&quot;Sum 3&quot;).append(&quot;&lt;/th&gt;&quot;).append(System.lineSeparator());
	// close the header row
	htmlTableBuilder.append(&quot;\t&quot;).append(&quot;&lt;/tr&gt;&quot;).append(System.lineSeparator());
	
	// add a data row for each PetrolData
	for (PetrolData pd : petrolDataList) {
		// open a row again
		htmlTableBuilder.append(&quot;\t&quot;).append(&quot;&lt;tr&gt;&quot;).append(System.lineSeparator());
		// add the data
		htmlTableBuilder.append(&quot;\t&quot;).append(&quot;\t&quot;).append(&quot;&lt;td&gt;&quot;).append(pd.getTitle()).append(&quot;&lt;/td&gt;&quot;).append(System.lineSeparator());
		htmlTableBuilder.append(&quot;\t&quot;).append(&quot;\t&quot;).append(&quot;&lt;td&gt;&quot;).append(pd.getSum1()).append(&quot;&lt;/td&gt;&quot;).append(System.lineSeparator());
		htmlTableBuilder.append(&quot;\t&quot;).append(&quot;\t&quot;).append(&quot;&lt;td&gt;&quot;).append(pd.getSum2()).append(&quot;&lt;/td&gt;&quot;).append(System.lineSeparator());
		htmlTableBuilder.append(&quot;\t&quot;).append(&quot;\t&quot;).append(&quot;&lt;td&gt;&quot;).append(pd.getSum3()).append(&quot;&lt;/td&gt;&quot;).append(System.lineSeparator());
		// close the row
		htmlTableBuilder.append(&quot;\t&quot;).append(&quot;&lt;/tr&gt;&quot;).append(System.lineSeparator());
	}
	// and close the table
	htmlTableBuilder.append(&quot;&lt;/table&gt;&quot;);
	
	// 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:

&lt;table border=&quot;1&quot;&gt;
&lt;tr&gt;
&lt;th&gt;Title&lt;/th&gt;
&lt;th&gt;Sum 1&lt;/th&gt;
&lt;th&gt;Sum 2&lt;/th&gt;
&lt;th&gt;Sum 3&lt;/th&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Petrol 1&lt;/td&gt;
&lt;td&gt;4.099999904632568&lt;/td&gt;
&lt;td&gt;8.199999809265137&lt;/td&gt;
&lt;td&gt;12.399999618530273&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Petrol 2&lt;/td&gt;
&lt;td&gt;5.300000190734863&lt;/td&gt;
&lt;td&gt;9.199999809265137&lt;/td&gt;
&lt;td&gt;13.239999771118164&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Petrol 3&lt;/td&gt;
&lt;td&gt;6.0&lt;/td&gt;
&lt;td&gt;10.100000381469727&lt;/td&gt;
&lt;td&gt;14.312000274658203&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Petrol 4&lt;/td&gt;
&lt;td&gt;7.699999809265137&lt;/td&gt;
&lt;td&gt;11.899999618530273&lt;/td&gt;
&lt;td&gt;15.149999618530273&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Petrol 5&lt;/td&gt;
&lt;td&gt;8.449999809265137&lt;/td&gt;
&lt;td&gt;12.0&lt;/td&gt;
&lt;td&gt;16.93600082397461&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Petrol 6&lt;/td&gt;
&lt;td&gt;9.670000076293945&lt;/td&gt;
&lt;td&gt;13.199999809265137&lt;/td&gt;
&lt;td&gt;17.777000427246094&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;

Pay attention to the format of doubles, you might have to apply a DecimalFormat or anything similar for correct values to be shown.

huangapple
  • 本文由 发表于 2020年8月26日 15:12:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/63592332.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定