How can I send email from a JSP page with HTML format?
Created May 7, 2012
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' %> </mt:message>
- 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.