如何在Java中通过电子邮件从数据库发送数据表?

问题描述

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

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表,则可以使用StringBuilder

基本上,您必须在循环浏览数据之前,先添加表格的开始标记和标题行 ,然后为每个PetrolData添加一行,并在循环之后添加表格的结束标记。

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

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或类似的东西才能显示正确的值。