2

I need to send an acknowledgement email to the client once they have made a booking. It will be HTML email for aesthetics.

The email message will include the data the user has put in.

I've already looked into the various guides on how to send the email.

But I need some pointers on how to contruct the HTML email message. I was thinking of jatl as it was mentioned in separate post. But unsure if this is the best way to go.

Thanks! :)

mrjayviper
  • 2,258
  • 11
  • 46
  • 82
  • 3
    Voting to close as not a real question - "how to construct" HTML is really vague. Dreamweaver? Write it by hand? Use a template library like Twig? etc... – djechlin Jan 24 '13 at 05:03
  • I did provide an example which is using the jatl library. http://code.google.com/p/jatl/source/browse/src/test/java/com/googlecode/jatl/HtmlBuilderTest.java – mrjayviper Jan 24 '13 at 05:03
  • What is your environment? Is your app stand alone? Does it run in a web server? It matters for my answer (but probably not for anybody elses) – Daniel Kaplan Jan 24 '13 at 05:15

4 Answers4

3

It sounds like you are wanting a template email which you can reuse, populating the user name and etc on the fly (as you send the email). I'd recommend that you have a look at http://www.stringtemplate.org/

String template works wonders for me.

noddy
  • 185
  • 1
  • 10
  • At first glance, that library looks like a bad fit. It seems to use < and > to wrap around variables. It seems like it'd get in your way when you're trying to render HTML. Please correct me if I'm wrong. I just took a cursory glance. – Daniel Kaplan Jan 24 '13 at 05:12
  • @titTYT - String template doesn't use the angle braces ('<' or '>') to wrap params so far as I'm aware. Check out http://www.antlr.org/wiki/display/ST/Examples for some examples. Let me know if I have missed something though. :-) – noddy Mar 01 '13 at 00:45
2

I would recommend you to go with a Velocity Template. You can construct the HTML email Template using Velocity Template and call it from you Java code using VelocityEngine.

You might want to look at this documentation for using Velocity

http://velocity.apache.org/

For more details, have a look at this url on how to use it for email template:

http://www.java2s.com/Code/Java/Velocity/UseVelocitytogenerateHTMLbasedemail.htm

zdesam
  • 2,936
  • 3
  • 25
  • 32
2

The way I've solved this before is by printing a JSP to a String and then sending that String as the body of the email. This is easy if you're in a webapp already (which I was). If you're not in a webapp, I don't recommend it.

Here's a stackoverflow answer on how to do it. And here's some sample code I use:

public static String generateEmailBodyAsString(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, ServletContext servletContext) throws Exception {
    StringWriter stringWriter = new StringWriter();
    final PrintWriter printWriter = new PrintWriter(stringWriter);
    final ServletOutputStream outputStream = new ServletOutputStream() {
        @Override
        public void write(int b) throws IOException {
            printWriter.write(b);
        }
    };

    HttpServletResponseWrapper wrapper = new HttpServletResponseWrapper(httpServletResponse) {
        @Override
        public PrintWriter getWriter() throws IOException {
            return printWriter;
        }

        @Override
        public ServletOutputStream getOutputStream() throws IOException {
            return outputStream;
        }
    };

    httpServletRequest.setAttribute("youCanAccessThisAsAVariableInYourJsp", "some value");     //in your jsp refer to it as ${youCanAccessThisAsAVariableInYourJsp}         
    servletContext.getRequestDispatcher("/emailBody.jsp").include(httpServletRequest, wrapper);

    return stringWriter.toString();
}

(Note: This code compiles but I've modified it to hide specifics of my app so it may not work for you at runtime. If you have any issues add a comment and I'll try to help)

Community
  • 1
  • 1
Daniel Kaplan
  • 62,768
  • 50
  • 234
  • 356
1

I always used Velocity for that kind of thing before, but when doing an OSX project in Objective-C I came across the Mustache template engine. . has a cool name and works pretty well. It's available in just about every language. Here's the java version: https://github.com/spullara/mustache.java

For the actual sending part, I recommend Spring over the pure javax.mail api (I always found that a bit puzzling - I guess it was designed for flexibility, rather than ease of use). . docs: http://static.springsource.org/spring/docs/2.0.8/reference/mail.html

Jasper Blues
  • 28,258
  • 22
  • 102
  • 185