How can I send email from a JSP page with HTML format?

michael barbarino

I've had a lot of success using the Jakarta mail tags. In my application I needed to send out customized email to various accounts and I wanted to do this using a simple template so that others could modify it easily. See: http://jakarta.apache.org/taglibs/index.html

The basic steps are:

  • build a JSP that uses the mail tags, specifying that the message type is HTML for the body of the message.
  • build a JSP that defines what the mail looks like.
  • the first JSP uses the JSP file include for the body of the message:
    <mt:message type="html">
    <%@ include file='mailTemplate.jsp' %>
  • the first JSP defines a bean at request scope to include name-value pairs to be used in the included file. I.e., for the first JSP:
    <jsp:useBean class='yourBean' 
    id='someBeanId' scope='request' />
    You can then use the bean to get name-value data to customize the email. Note that you won't 'use bean' in the included JSP.
This whole structure can be embedded in a 'repeat' tag, giving you the ability to send out multiple emails, each customized.