JavaWriting a servlet for sending e-mail

Writing a servlet for sending e-mail

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

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.

Tools

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.

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:

  • 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.

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, 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 SendMailTest.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:

  • With your text editor, edit the HTML that you find here, change the URL “http://t-rose:8080/SendMailServlet” to what is appropriate for your Web server, then save the file as SendMailTest.html in a Web directory of your server. (For example, using JRun with IIS would result in a URL that looks like “http://mycompany.com/ servlet/SendMailServlet”, and the file would be saved in “C:inetpubwwwroot”.)

  • Copy SendMailServlet.class to the servlet directory for your Web server. (For example, with JRun and IIS, you would copy SendMailServlet.class to “C:JRunservlets”.)

  • Start your browser and point it to “http://// 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 that looks like this.

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


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 trose@avana.net.


Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories