File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes Compose Mails using JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Compose Mails using JSP" Watch "Compose Mails using JSP" New topic

Compose Mails using JSP

Andreas Schildbach
Ranch Hand

Joined: Jan 22, 2003
Posts: 34
Hello everyone,
I'd like to send highly customized mails to the users of my web site. As a already use JSP to customize web pages for my users, it would be great to also use JSP for composing the mails.
Has anyone already done this?
I'd like to primarely generate text messages, but maybe also HTML formatted mails if the users ask for it. My idea is use JSP + JSTL Tag Library to compose the content and then use an XSLT style sheet to layout the mail as plain text or HTML.
Are there any best practises?
Are there any alternatives to using JSP?
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
Have u tried to use JavaMail API? Or is it not fitted to your spec? If you got any problem using JavaMail API, u will find the JavaMail forum is very useful for you...

Co-author of SCMAD Exam Guide, Author of JMADPlus
Axel Janssen
Ranch Hand

Joined: Jan 08, 2001
Posts: 2164
I don't see sending email as a frontend process, though there might be ready-to-use taglibs for the job.

So it might be better to use objects in business layer for the actual text composition, layout, sending.
Users can choose their layout preferences in a jsp page. This information is stored in some persistence storage medium like a database or a xml file. Objects in business layer can access those information.
A job scheduler like quartz might be a usefull helper for the task.
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982
Besides using JavaMail API to compose the mail, you may also need JAF API (Java Activation Framework) for sending the email.
Basically, this is not a difficult task. I also dont know whether there are custom tags doing this, but if you really need a practice, you may consider MVC.
Using JSP to display and capture the form parameters for the email content, send it to the Servlets, and the Servlet is responsible for constructing the email using JavaMail API, and send it to the receivers via JAF API. In addition, the receiver list can also be obtained from the database, so you may also need JDBC calls.
Hope this help.

SCJP 1.2, OCP 9i DBA, SCWCD 1.3, SCJP 1.4 (SAI), SCJD 1.4, SCWCD 1.4 (Beta), ICED (IBM 287, IBM 484, IBM 486), SCMAD 1.0 (Beta), SCBCD 1.3, ICSD (IBM 288), ICDBA (IBM 700, IBM 701), SCDJWS, ICSD (IBM 348), OCP 10g DBA (Beta), SCJP 5.0 (Beta), SCJA 1.0 (Beta), MCP(70-270), SCBCD 5.0 (Beta), SCJP 6.0, SCEA for JEE5 (in progress)
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
In addition to what has already been said, which is good advice, you could indeed use a JSP as an email template for your email messages.
What you'd do is have a servlet send an HTTP request to the JSP and capture the returned HTML which it can then use as the body of the email (after stripping headers of course).
I would avoid calling the service method on the JSP directly (though it may be technically possible) as that would likely break any hope of cross-server portability and might not work at all depending on how your appserver constructs the classname of the generated JSP class.

Axel Janssen
Ranch Hand

Joined: Jan 08, 2001
Posts: 2164
Andreas, you might tell us more about the business logic of those email.
What are they good for? What's their purpose in your website from a functionality standpoint.
Think you will find lots of eager respondents this week
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

one alternative to using JSP ... a package like Velocity. ( )

Velocity is strictly a template and merging engine. So your flow would be something like:

1) gather the data required to produce a customized email (from database, flatfiles, etc).
2) select a template to use (plain text, html)
3) merge the data with the template
4) use this merged output as the body of the email
5) repeat from step 1, as many times as necessary.

the advantage of using a system like this, is that you don't need to construct an HttpRequest, in order to 'call' your JSP page. No URLConnection or proxying required.

Being a velocity template, it can also be used 'offline', from a command line, as a scheduled job, etc. etc. That is: it is not coupled to the web in any way. Templates, and their parsed representation are cached in memory, so it is also very fast.

My own app uses Velocity for exactly this purpose (sending out emails), as well as apps like
Andreas Schildbach
Ranch Hand

Joined: Jan 22, 2003
Posts: 34
Thanks everyone!
There is no question I will use JavaMail/JAF for actually sending/transporting the e-Mail to the MTA. My posting is targetting only the composition of the message content, like
- mixing dynamic content with static content using maybe a template system
- localisation to the users language
- applying a style/layout (plain text / html)
I am developing a chess game server ( The system sends notifications at several events, most of them are generated by a users HTTP request (not necessarely the same user that gets the mail), some of them generated by a cron job that calls special service URLs not accessible for the users. Notifications include:
- Game/Tournament has started: Players and game conditions are listed
- It's your turn: Turn of other player and game history is listed
- Game ended: Outcome/Resolution of the Game, Player Ratings Adjusted
All of these notifications include links to web content for easy access.
The first generation sent unpersonalized mails in two languages: "It's your turn, you can take your turn at: ..."
The second generation personalized plain text mails by using java.util.PropertyResourceBundle's for localized java.text.MessageFormat's. As all of my JSP pages store its localized strings in the same ResourceBundles anyway, it was a handy way to go.
But customisation is more than just substituting {0} with a user name and the like. MessageFormats don't support iterations, like Player lists. I started to use MessageFormats inside MessageFormats. This is why I am thinking about scrapping the concept and implementing the third generation.
Mike, thanks for your pointer to Velocity. Unfortunately the rest of the application already uses JSP so there won't be any synergy between web pages and a message.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: Compose Mails using JSP
jQuery in Action, 3rd edition