0

I need to send emails formatted with HTML. It seems an HTML email counts as a "view" element so it makes sense to render it using JSP.

However, the emailer task would be written in pure Java. How can I get the JSP output into Java?

i.e. I envision:

emailbody.jsp:

<c:out var="invoicebody">
    <lots of html>
    ${invoice.price} etc...
</c:out>

emailsend.java:

setup db connection
setup email
call emailbody.jsp
email.body = invoicebody
email.send etc...

Something like that...

Right now I am scraping it through the http server but that just seems wrong.

What is the best way to format HTML emails? Using Tomcat 7, Servlet 3.0...

Thanks

PrecisionPete
  • 3,139
  • 5
  • 33
  • 52
  • Is the emailer tasker within your web app? Did you consider using HttpURLConnection ? – rickz Mar 04 '13 at 15:33
  • http://stackoverflow.com/questions/1075827/execute-jsp-directly-from-java/1076056#1076056 – Szymon Jednac Mar 05 '13 at 11:24
  • I am trying to avoid sucking the jsp through the http server. That can't be the best way. I also don't want the jsp to be externally accessible. Nor do I want it to depend on the The Mock Objects concept seem to be interesting... Might be my best bet... – PrecisionPete Mar 05 '13 at 18:18

3 Answers3

1

You can use a Scripting language like MVEL to populate the HTML file that you want to send out as Email.

Check this MVEL 2.0 Templating Guide

I hope this puts you in right direction.

There are also other alternatives as Szymon Biliński pointed out.Listing them below

Sudhakar
  • 4,823
  • 2
  • 35
  • 42
  • Also seems interesting. I didn't want to introduce another templating language since JSP pretty much is that. But I like that it's no longer dependent on the http server. – PrecisionPete Mar 05 '13 at 18:23
1

You can call a URL of your own Webapplication like this:

    public static String getEmailBody(final String triggerId) throws IOException {

        URL url = new URL("http://localhost:8080/emailbody.jsp?triggerId="
                + triggerId);
        URLConnection openConnection = url.openConnection();
        openConnection.setReadTimeout(100);
        InputStream inputStream = openConnection.getInputStream();
        int read;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        do {
            byte[] bytes = new byte[1024];
            read = inputStream.read(bytes);
            baos.write(bytes);
        } while (read >  0);
        return baos.toString();
    }

This is the most detached methode because you have an "real" read httprequest and httpresponse.

Grim
  • 1,938
  • 10
  • 56
  • 123
0

There already is a good solution using taglibs for Mail-Sending.

Just call the jsp, you dont need to capture the html.

Grim
  • 1,938
  • 10
  • 56
  • 123
  • Trying to get it out of the JSP. It slows the app execution, is not logged, and is not retriggerable if something goes wrong. e.g. no SMTP relay. Instead, I put requests in a database and have a batch job to send emails. Since the batch job is not a web page, I don't want to run it through the web server. – PrecisionPete Mar 05 '13 at 18:21
  • ... and you can not Mock it, you can hardly debug and there is no good solution for blacklists and on and on. Well its an simple implementation and does not fit such a need. I didnt know that needs, i will create a new Answer. – Grim Mar 06 '13 at 06:55