StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
String rowFormat = "%8s %8s %8s %8s%n";
pw.printf(rowFormat, "Col A", "Col B", "Col C", "Col XY");
pw.printf(rowFormat, "A", "19", "Car", "55");
pw.printf(rowFormat, "X", "21", "Train C", "-4");
pw.printf(rowFormat, "B", "-9", "Bike", "0");
String message = sw.toString();
System.out.println(message);
The above program prints the following table and the alignment is perfect.
Col A Col B Col C Col XY
A 19 Car 55
X 21 Train C -4
B -9 Bike 0
Instead of printing the string message
, I am passing the string to another function which sends email with the content of the mail as the string message
.
mail.setContent(message.toString(),"text/plain");
This is how I am setting the content as message in my mail.
But the alignment of the table is not appropriate as I do receive while pri开发者_运维知识库nting the string. What must be done such that I receive the content of my mail as similar as the printing format of the string. Please advise on this regard.
Its good to build a HTML
markup for this, i.e. <table>
, and set that in your mail
like this,
mail.setContent(message.toString(),"text/html);
The email looks fine to me (on Outlook). What email client are you using? Maybe it is applying formatting to the message, even though it is Plain Text.
精彩评论