How to find the path to a .txt file in glassfish v3.0

In my application, I want to send an html template to users email address. Everithing works correctly when I programmatically create html, but now I want it to read the html text from a file in my application and send it. I get a FileNotFoundException and I don't know how to find this .txt file. See code:

public void sendAccountActivationLinkToBuyer(String destinationEmail,
        String name) {

    // Destination of the email
    String to = destinationEmail;
    String from = "myEmail@gmail.com";

    try {
        Message message = new MimeMessage(mailSession);
        // From: is our service
        message.setFrom(new InternetAddress(from));
        // To: destination given
        message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse(to));
        message.setSubject("Registration succeded");
        // Instead of simple text, a .html template should be added here!
        message.setText(generateActivationLinkTemplate());

        Date timeStamp = new Date();
        message.setSentDate(timeStamp);

        Transport.send(message);

    } catch (MessagingException e) {
        throw new RuntimeException(e);
    }

}

private String generateActivationLinkTemplate() {
    String htmlText = "";

    try {
        File f = new File("");
        BufferedReader br = new BufferedReader(new InputStreamReader(f.getClass().getResourceAsStream("./web/emailActivationTemplate.txt")));
        String content = "";
        String line = null;

        while ((line = br.readLine()) != null) {
            content += line;
        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return htmlText;
}

The second method gives me problems, I can not find this .txt file. What should I do? I created a folder in the WebContent folder, now the web folder is next to META-INF and WEB-INF (I think this is a good place to store my images, templates, css ...) Inside the folder I manually inserted emailActivationTemplate.txt Now I need to read it. Any ideas?

This is the console output:

SEVERE: java.io.FileNotFoundException:.\web\emailActivationTemplate.txt( )

0
3

emailActivationTemplate.txt WEB-INF/classes

BufferedReader br = new BufferedReader(new InputStreamReader(Thread.currentThread().getContextClassLoader().getResource("emailActivationTemplate.txt"));
+3
(String) System.getProperties().get("com.sun.aas.instanceRoot")
+3

Your emailActivationTemplate.txtmust be present inside the folder classes WEB-INF. If you manage to post it there, you should read it using:

BufferedReader br = new BufferedReader(new InputStreamReader(this.getClass().getClassLoader().getResourceAsStream("/emailActivationTemplate.txt")));

Try without specifying the leading '/' if it does not work.

+1
source

All Articles