有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

html如何在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>";
            }

显然,这对我不起作用,我能做什么


共 (1) 个答案

  1. # 1 楼答案

    如果希望在不使用任何外部库的情况下在行中创建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或任何类似的格式才能显示正确的值