如何在 java 中通过电子邮件从数据库发送数据 (table)?

How to send data(table) from database via email in java?

我可以发送常规电子邮件(不是来自数据库),但我如何才能在电子邮件中发送完整的 table? 这是我的代码:

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;
}

我试过像这样简单的方法:

     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>";
            }

显然,它对我不起作用,我该怎么办?

如果您想在不使用任何外部库的情况下连续创建一个 HMTL table,您可以使用 StringBuilder

您基本上必须添加 table 和 header 行的起始标记 循环数据之前,然后添加一行对于每个 PetrolData 并在循环之后添加 table.

的结束标记

查看此示例(它可能使用了不必要的换行符和制表符,但它们对于示例输出很有用,因为我不会在此处发送电子邮件):

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());
}

输出如下,您可以轻松地将其嵌入有效的 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 或任何类似的格式才能显示正确的值。