Generic Service – HTML Form Post to Email Servlet

2

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!

 

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:

&lt;form action=&quot;http://10.0.0.156:8988/COA_WEB-ViewController-context-root/htmlpost2emailservlet&quot; <br /> method=&quot;post&quot; enctype=&quot;application/x-www-form-urlencoded&quot;&gt;<br />            &lt;input type=&quot;hidden&quot; name=&quot;email_addressee&quot;<br />                   value=&quot;jellema@amis.nl&quot;/&gt;<br />            &lt;input type=&quot;hidden&quot; name=&quot;email_subject&quot;<br />                   value=&quot;Conference Registration&quot;/&gt;<br />            &lt;input type=&quot;hidden&quot; name=&quot;email_sender&quot;<br />                   value=&quot;COA-WEB&quot;/&gt; <br />

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 (jellema@amis.nl) 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.

 

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

HTMLFormPost2EmailServlet

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;<br /><br />import java.io.IOException;<br />import java.io.PrintWriter;<br />import java.util.Enumeration;<br /><br />import javax.servlet.*;<br />import javax.servlet.http.*;<br /><br />public class HtmlPost2EmailServlet extends HttpServlet {<br />    private static final String CONTENT_TYPE = <br />        &quot;text/html; charset=windows-1252&quot;;<br /><br />    public void init(ServletConfig config) throws ServletException {<br />        super.init(config);<br />    }<br /><br />    public void doPost(HttpServletRequest request, <br />                       HttpServletResponse response) throws ServletException, <br />                                                            IOException {<br />        String emailAddressee = &quot;&quot;;<br />        String emailSubject = &quot;&quot;;<br />        String emailSender = &quot;&quot;;<br />        String content = &quot;&quot;;<br />        emailAddressee = request.getParameter(&quot;email_addressee&quot;);<br />        emailSubject = request.getParameter(&quot;email_subject&quot;);<br />        emailSender = request.getParameter(&quot;email_sender&quot;);<br /><br />        Enumeration paramNames = request.getParameterNames();<br />        while (paramNames.hasMoreElements()) {<br />            String paramName = (String)paramNames.nextElement();<br />            String paramValue = &quot;&quot;;<br />            String[] paramValues = request.getParameterValues(paramName);<br />            if (paramValues.length == 1) {<br />                paramValue = paramValues[0];<br />            } else {<br />                for (int i = 0; i &lt; paramValues.length; i++) {<br />                    paramValue = paramValue + (i &gt; 0 ? &quot;,&quot; : &quot;&quot;) + paramValues[i];<br />                } // for<br />            }<br />            content = content + paramName + &quot;=&quot; + paramValue + &quot;\n&quot;;<br />        } //while<br /><br /><br />        response.setContentType(CONTENT_TYPE);<br />        PrintWriter out = response.getWriter();<br /><br />        out.println(&quot;&lt;html&gt;&quot;);<br />        out.println(&quot;&lt;head&gt;&lt;title&gt;HtmlPost2EmailServlet&lt;/title&gt;&lt;/head&gt;&quot;);<br /><br />        try {<br />            EmailSender.sendEmail(emailAddressee, emailSubject, emailSender, <br />                                  content);<br />            out.println(&quot;&lt;body onload='alert(\&quot;Your registration was received successfully. Thank you! \&quot;);'&gt;&quot;);<br />            out.println(&quot;The email has been sent. &lt;a href='javascript:history.go(-1);'&gt;Return to previous page.&lt;/a&gt; &quot;);<br />        } catch (Exception e) {<br />            out.println(&quot;&lt;body onload='alert(\&quot;Unfortunately, an error occurred while processing your request! \&quot;);'&gt;&quot;);<br />            out.println(&quot;Could you please try again. &lt;a href='javascript:history.go(-1);'&gt;Return to previous page.&lt;/a&gt; &quot;);<br /><br />        }<br /><br />        out.println(&quot;&lt;/body&gt;&lt;/html&gt;&quot;);<br />        out.close();<br />    }<br />}<br />&nbsp;

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

The Emailer Class:

<br />package nl.amis.util;<br /><br />import java.util.Properties;<br /><br />import javax.mail.Address;<br />import javax.mail.Message;<br />import javax.mail.Session;<br />import javax.mail.Transport;<br />import javax.mail.internet.InternetAddress;<br />import javax.mail.internet.MimeMessage;<br /><br /><br />public class EmailSender {<br />    public EmailSender() {<br />    }<br /><br />    public static void sendEmail( String to, String subject, String from, String content) throws Exception{<br />        Properties props = new Properties();<br />        props.setProperty(&quot;mail.transport.protocol&quot;, &quot;smtp&quot;);<br />        props.setProperty(&quot;mail.host&quot;, &quot;mailhost.amis.nl&quot;);<br />        props.setProperty(&quot;mail.user&quot;, &quot;mail_user&quot;);<br />        props.setProperty(&quot;mail.password&quot;, &quot;&quot;);<br /><br />        Session mailSession = Session.getDefaultInstance(props, null);<br />        Transpo
rt transport = mailSession.getTransport(
);<br /><br />        MimeMessage message = new MimeMessage(mailSession);<br />        message.addFrom(new Address[] { new InternetAddress(&quot;jellema@amis.nl&quot;,from)});  // the reply to email address and a logical descriptor of the sender of the email!<br /><br />        message.setSubject(subject);<br />        message.setContent(content, &quot;text/plain&quot;);<br />        message.addRecipient(Message.RecipientType.TO,<br />             new InternetAddress(to));<br /><br />        transport.connect();<br />        transport.sendMessage(message,<br />            message.getRecipients(Message.RecipientType.TO));<br />        transport.close();<br />        }<br />        <br />        public static void main (String[] args) throws Exception {<br />            String content = &quot;Some test content.&quot;;<br />            EmailSender.sendEmail(&quot;jellema@amis.nl&quot;,&quot;An interesting message&quot;,&quot;THE APP&quot;,content);<br />        }<br />   }<br /><br />
Share.

About Author

Lucas Jellema, active in IT (and with Oracle) since 1994. Oracle ACE Director for Fusion Middleware. Consultant, trainer and instructor on diverse areas including Oracle Database (SQL & PLSQL), Service Oriented Architecture, BPM, ADF, Java in various shapes and forms and many other things. Author of the Oracle Press book: Oracle SOA Suite 11g Handbook. Frequent presenter on conferences such as JavaOne, Oracle OpenWorld, ODTUG Kaleidoscope, Devoxx and OBUG. Presenter for Oracle University Celebrity specials.

2 Comments

  1. Sam, It is not hugely different, except for a few things:
    – it is supported by all browsers, as it does not rely on the widely varying implementation of the mailto action
    – it creates – depending on the servlet of course – a nicer email message (not just one long line with all data); an enhanced version of the servlet can even create an XML document in the Email body
    – I did not know about the mailto action until you wrote your comment. So thanks for the enlightenment.

    best regards,

    Lucas