Writing a Servlet for Sending E-mail

Thornton Rose

Published as "Writing a Servlet for Sending E-mail", 6/11/98, Gamelan.com.

Copyright © 2001, Thornton Rose.


In my previous article, Writing Active Server Components in Visual Basic, I showed you how to write a Visual Basic component that plugs into Microsoft's Web server, the Internet Information Server (IIS). In this article, I will show you how to write a servlet, a Java component that plugs into a web server, that can be used to send e-mail from a Web page.

Tools

Here is what you will need to write a servlet:

The Code

To write a servlet for sending e-mail from a Web page, first you extend javax.servlet.http.HttpServlet, then implement a few methods for servicing the Web page request, and finally implement a few more methods for communicating with an e-mail server. Here is the code.

You will notice that SendMailServlet has only seven small methods. The methods used to service the Web page request are:

  1. service(HttpServletRequest request, HttpServletResponse response)

    This method is inherited from HttpServlet and is called when the servlet is invoked (for both GET and POST requests). It gets the parameters from the HTTP request, sends the e-mail, and then sends a response page back to the user.

  2. getParameter(HttpServletRequest request, String name)

    This method is used to retrieve the value of the named parameter from the HttpRequest object.

  3. sendResponse(HttpServletRequest request, HttpServletResponse response, String statusMessage, Vector sessionTrace)

    This method is used to send a response page, formatted in HTML, back to the user.

These methods are used for sending e-mail:

One final note: You will notice that I did not use any member variables and instead passed all data from service() to the other methods. I did this because one instance of SendMailServlet is created by the Web server, and thereafter, that instance must service multiple, and possibly simultaneous, page requests.

Compilation

Compiling SendMailServlet is easy:

  1. Grab the code for SendMailServlet and save it in a file called "SendMailServlet.java."

  2. Start a shell and go to the directory where you put SendMailServlet.java.

  3. Assuming the JDK is in your path, enter the command "javac SendMailServlet.java". This should produce a class file called SendMailServlet.class.

Testing

Testing is not as easy as compilation, because it depends on whether you are using ServletRunner or a Web server. Here is how you test with ServletRunner:

  1. With your text editor, edit the HTML that you find here [link to SendMailTest.html.txt], change "t-rose" to the name or IP address of your machine, then save the file as "SendMailTest.html" in the same directory with SendMailServlet.class.

  2. At a command prompt in the directory where you compiled SendMailServlet, run the ServletRunner tool by entering the command "servletrunner -v".

  3. Start your browser and open the file SendMail.html. The page you see should look like this.

  4. Click the Send button. If the servlet runs OK, you should get back a page the looks like this.

Here is how you test with a Web server:

SMTP

SMTP (Simple Mail Transfer Protocol) is the protocol that is used to transport e-mail over the internet. For a complete description of it, you can refer to RFC 821. Basically, though, you open a socket to a mail server on port 25, then alternately write strings of the form

COMMAND data

to the socket and read strings of the form

nnn message

from the socket. A typical session looks like this.

Related Links