简体   繁体   中英

Send data from java mail in HTML Table

I am trying to send data from my Java mail application in the form of a HTML table. The problem is that when I receive the mail, the data is in simple text format and not in a table form. Please suggest a solution

StringBuilder sb1 = new StringBuilder(200);
sb1.append("<html><body>"+ "<table style='border:2px solid black'>");
for(SCSINPojo cat : ar){

    sb1.append("<tr>");
    sb1.append(cat.getINNumber())
       .append("<td>")
       .append(cat.getDescription())
       .append("</td><td>")
       .append(cat.getStatus())
       .append("</td><td>")
       .append(cat.getStatus())
       .append("</td><td>")
       .append(cat.getOwner())
       .append("</td><td>")
       .append(cat.getOwnerGroup())
       .append("</td><td>")
       .append(cat.getSeverity());
    sb1.append("</tr>");
    String in = sb1.toString();

}
sb1.append("</table></body></html>");

message.setText(sb1.toString());

Transport.send(message);

Try this -

MimeMessage simpleMessage = new MimeMessage(mailSession);

Then, when you want to set the message body, either call

simpleMessage.setText(text, "utf-8", "html");

or call

simpleMessage.setContent(text, "text/html; charset=utf-8");

Hope this will help you :)

Try this...you can even print it out to an HTML file and so I have added some break lines for appearance.

sb1.append("<html><body><br/>"+ "<table style='border:2px solid black'><br/>");
    for(SCSINPojo cat : ar){

        sb1.append("<tr><br/><td>");
        sb1.append(cat.getINNumber())
           .append("</td><br/><td>")
           .append(cat.getDescription())
           .append("</td><br/><td>")
           .append(cat.getStatus())
           .append("</td><br/><td>")
           .append(cat.getStatus())
           .append("</td><br/><td>")
           .append(cat.getOwner())
           .append("</td><br/><td>")
           .append(cat.getOwnerGroup())
           .append("</td><br/><td>")
           .append(cat.getSeverity());
        sb1.append("</td><br/><td>");
        String in = sb1.toString();

    }
    sb1.append("</table></br></body></html>");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM