June 11, 1998
Writing a servlet for sending e-mail
by 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 or Java component that plugs into a Web server and can send e-mail from a Web page.
Here is what you will need to write a servlet:
Your favorite text editor,
The Java Development Kit (JDK) 1.2 Beta 3 or JDK 1.1.x with the Java Servlet Development Kit (JSDK). Note that if you do not already have the JSDK, you will have to download JDK 1.2 Beta 3 because JavaSoft has discontinued the JSDK as a standalone product.
Access to a mail server that uses SMTP (Simple Mail Transfer Protocol),
Optionally, you might benefit from a Web server that supports the Java Servlet API. Currently, the only Web server that does this directly is the Java Web Server from Sun. If you have Apache, Netscape Server, IIS, or WebSTAR, you can get the JRun Servlet Engine for free from Live Software. This will add servlet support to your web server. However, you only need the web server for deployment. For testing, you can use the ServletRunner tool that comes with the JDK.
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:
- 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.
getParameter(HttpServletRequest request, String name)
This method is used to retrieve the value of the named parameter from the HttpRequest object.
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:
- sendMail (String host, String domain, String sender, String recipients, String subject, String maildata, Vector sessionTrace)
This method is used to send the e-mail using SMTP. (I will explain more about SMTP after compilation and testing.)
sendCommand (DataOutputStream out, String command, Vector sessionTrace)
This method is used to send a command to the mail server. It writes a string to the given output stream and saves the command in a list for debugging purposes.
readReply (BufferedReader reader, Vector sessionTrace)
This method is used to read a reply from the mail server. It reads a string from the given input reader (stream) and saves the reply in a list for debugging purposes.
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.
Compiling SendMailServlet is easy:
- Grab the code for SendMailServlet and save it in a file called "SendMailServlet.java."
Start a shell and go to the directory where you put SendMailServlet.java.
Assuming the JDK is in your path, enter the command "javac SendMailServlet.java". This should produce a class file called SendMailServlet.class.
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:
- With your text editor, edit the HTML that you find here, 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.
At a command prompt in the directory where you compiled SendMailServlet, run the ServletRunner tool by entering the command "servletrunner -v".
Start your browser and open the file SendMailTest.html. The page you see should look like this.
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 (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
to the socket and read strings of the form
from the socket. A typical session looks like this.
Links on this article
Thornton Rose currently works in the MIS department at AGCO Corporation, where he is a software developer and the Webmaster for the Intranet. He can be reached via e-mail at firstname.lastname@example.org.