Search code examples
jakarta-mail

Sending htmlcontent using java mail


I am trying to send the below html content to the body of the mail. The content is displaying only data . No border . No background colour for table header. When I execute the the html content below seperately it displays all of these. Please help

StringBuilder builder = new StringBuilder();
                builder.append("<!DOCTYPE html>");
                builder.append("<html>");
                builder.append("<head>");
                builder.append("<style>");
                builder.append("table {");
                builder.append("width:100%;");
                builder.append("}");
                builder.append("table, th, td {");
                builder.append("border: 1px solid black;");
                builder.append("border-collapse: collapse;");
                builder.append("}");
                builder.append("th, td {");
                builder.append("padding: 5px;");
                builder.append("text-align: left;");
                builder.append("}");
                builder.append("table th    {");
                builder.append("background-color: lightblue;");
                builder.append("color: black;");
                builder.append("}");
                builder.append("</style>");
                builder.append("</head>");
                builder.append("<body>");
                builder.append("<table >");
                builder.append("<tr>");
                builder.append("<th>First Name</th>");
                builder.append("<th>Last Name</th>  ");
                builder.append("<th>Points</th>");
                builder.append("</tr>");
                builder.append("<tr>");
                builder.append("<td>Jill</td>");
                builder.append("<td>Smith</td>  ");
                builder.append("<td>50</td>");
                builder.append("</tr>");\
                builder.append("</table>");
                builder.append("</body>");
                builder.append("</html>");
                String report = builder.toString();                     
                message.setContent(report,"text/html; charset=utf-8");
               Transport.send(message);

Borders are not getting displayed in the mail. Is there any fine example in which a table is sent to the email body that shows all the details including colours borders perfectly .


Solution

  • The issue is solved after i added specific css styles to the table border .