views:

21

answers:

2
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 printing 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.

+1  A: 

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);
Adeel Ansari
Am handling all the content of the mail in a single String and passing that to the email function. Hence am asking for the possibilities of modifying the current program.
LGAP
@LGAP: Of course, modify your program to use HTML mark-up to format. Otherwise, you can always have formatting issue, with different font faces and font sizes.
Adeel Ansari
Okay.. i will try the same and let you know of my updates. Thanks
LGAP
A: 

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.

dogbane
I am not using any email client like Outlook.
LGAP
Then how are you viewing the email?
dogbane
In browser.....
LGAP