Generic Service – HTML Form Post to Email Servlet

This article is about a generic service that takes a simple HTML Form Post and turns it into an Email. It is not revolutionary – but useful nevertheless. Besides, it is the first installment in a series that works towards a SOA implementation. We will implement a Business Process in this series using Oracle BPEL PM 10.1.3. The first step in this process is a Web Site (in this case even a static HTML document), that posts a form with the collected user input. The Form Submit – and therefore the sent email – is the trigger that starts the process.

We will see in this article how we can post an HTML form to any URL, in this specific case one that represents a generic Java Servlet but in the exact same manner also a PHP, PERL, ASP.NET or any other CGI like web application, that turns it into a an email. The HTML Document is completely decoupled from this Email Service – very SOA style!


The business case under scrutiny in this series revolves round the Conference Organisation Administration (COA). Conference visitors can register for the conference on the website. This registration triggers the backoffice process – by sending an email – crude but effective and still quite common!

 Generic Service - HTML Form Post to Email Servlet coaForm

This website – in this example represented by a static HTML document – allows users to register for the (fictitious) SOA and BPEL Conference.

The HTML Form in this static document posts to some generic servlet, somewhere on the internet:

<form action="" 
 method="post" enctype="application/x-www-form-urlencoded">
            <input type="hidden" name="email_addressee"
            <input type="hidden" name="email_subject"
                   value="Conference Registration"/>
            <input type="hidden" name="email_sender"

This Form must contain at least three parameters that are used by the servlet to send an email containing all form values posted by the conference registeree. In this case – my own email address ( is the one to which the email composed from the HTML Form Post is sent.

The servlet processes the Posted Parameters and composes an email that is subsequently sent to the addressee specified in the form. This likely an email account that is monitored by a backoffice clerk, the BPEL process activation agent – or a more simplistic poller – that initiates the back office process.

The email is sent and received by the Back Office.

Generic Service - HTML Form Post to Email Servlet emailHeader

It contains the values of all parameters in the Registration Form:

Generic Service - HTML Form Post to Email Servlet emailBody


The Servlet taking care of the processing of the HTML Form Post into an email is quite simple. We have the Servlet itself that makes use of an even simpler Emailer Class. Note that functionality was the primary aim of this code – more so than elegant and properly refactored code design.

The servlet:

package nl.amis.util;

import java.util.Enumeration;

import javax.servlet.*;
import javax.servlet.http.*;

public class HtmlPost2EmailServlet extends HttpServlet {
    private static final String CONTENT_TYPE = 
        "text/html; charset=windows-1252";

    public void init(ServletConfig config) throws ServletException {

    public void doPost(HttpServletRequest request, 
                       HttpServletResponse response) throws ServletException, 
                                                            IOException {
        String emailAddressee = "";
        String emailSubject = "";
        String emailSender = "";
        String content = "";
        emailAddressee = request.getParameter("email_addressee");
        emailSubject = request.getParameter("email_subject");
        emailSender = request.getParameter("email_sender");

        Enumeration paramNames = request.getParameterNames();
        while (paramNames.hasMoreElements()) {
            String paramName = (String)paramNames.nextElement();
            String paramValue = "";
            String[] paramValues = request.getParameterValues(paramName);
            if (paramValues.length == 1) {
                paramValue = paramValues[0];
            } else {
                for (int i = 0; i < paramValues.length; i++) {
                    paramValue = paramValue + (i > 0 ? "," : "") + paramValues[i];
                } // for
            content = content + paramName + "=" + paramValue + "\n";
        } //while

        PrintWriter out = response.getWriter();


        try {
            EmailSender.sendEmail(emailAddressee, emailSubject, emailSender, 
            out.println("<body onload='alert(\"Your registration was received successfully. Thank you! \");'>");
            out.println("The email has been sent. <a href='javascript:history.go(-1);'>Return to previous page.</a> ");
        } catch (Exception e) {
            out.println("<body onload='alert(\"Unfortunately, an error occurred while processing your request! \");'>");
            out.println("Could you please try again. <a href='javascript:history.go(-1);'>Return to previous page.</a> ");



The result as presented to the user after posting the registration form:

Generic Service - HTML Form Post to Email Servlet emailSent

The Emailer Class:

package nl.amis.util;

import java.util.Properties;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class EmailSender {
    public EmailSender() {

    public static void sendEmail( String to, String subject, String from, String content) throws Exception{
        Properties props = new Properties();
        props.setProperty("mail.transport.protocol", "smtp");
        props.setProperty("", "");
        props.setProperty("mail.user", "mail_user");
        props.setProperty("mail.password", "");

        Session mailSession = Session.getDefaultInstance(props, null);
rt transport = mailSession.getTransport(

        MimeMessage message = new MimeMessage(mailSession);
        message.addFrom(new Address[] { new InternetAddress("",from)});  // the reply to email address and a logical descriptor of the sender of the email!

        message.setContent(content, "text/plain");
             new InternetAddress(to));

        public static void main (String[] args) throws Exception {
            String content = "Some test content.";
            EmailSender.sendEmail("","An interesting message","THE APP",content);


  1. Lucas Jellema April 10, 2006
  2. Sam Pullara April 10, 2006