Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

New line Command (\n) was not working while handline files with TomCat

I am trying a to send a mail by reading contents from text file.

File I am trying to read

Line 1
Line 2
Line 3

I have tested the code with JUnit and I get the mail with same formatting as in the text file.

Output mail using Junit

Line 1
Line 2
Line 3

But when deployed in Tomcat, I get a mail with the text but new lines are not getting recognized.

Output mail when using tomcat server

Line 1Line 2Line 3

Code that I am using to read the contents of the file.

private String readFile(String pathname) throws IOException {
        File file = new File(pathname);
        StringBuilder fileContents = new StringBuilder((int) file.length());
        Scanner scanner = new Scanner(file);
        String lineSeparator = System.getProperty("line.separator");

        try {
            while (scanner.hasNextLine()) {
                fileContents.append(scanner.nextLine() + lineSeparator);
            }
            return fileContents.toString();
        } finally {
            scanner.close();
        }
    }

I have put the server in debug mode and I can see that new line is getting appended in fileContents.toString()

I am not sure what I am missing. Anything related to character set? I am not sure....

like image 303
om39a Avatar asked Nov 12 '22 14:11

om39a


1 Answers

It has something to do with clue from Perception and nullix. How the mail is read and the HTML.

In my case, I was testing by sending mail from a mail client which was sending out "text/html" type messages to my application. So when I am replying to that mail, the reply mail was also with "text/html" format. Hence the /n or what ever line feed type that I used was omitted.

So while reading the mail I read only "text/plain" mime type. So while replying to this mail, I could get the new line printed.

like image 52
om39a Avatar answered Nov 15 '22 05:11

om39a