Have you ever wanted to have your own Web e-mail system, rather than relying on free Web e-mail services? In this article I'll show you how to build a scalable Web e-mail system based on Java servlets and JavaMail, two members of the Java 2 Enterprise Edition (J2EE) platform. The system provides a thin, HTML-based e-mail client that will enable you to access your own e-mail account from any Web browser, anywhere in the world.
Java Servlets
Servlets have become a popular choice for building interactive Web applications. A servlet can be thought of as a faceless applet that runs on the server side. Numerous third-party servlet engines and servlet-enabled application servers are on the market today. Servlets provide a rich, platform-independent set of APIs for building Web-based applications without the performance limitations of CGI programs. The central point of the Servlets API is the Servlet interface, which needs to be implemented by the servlet host engine. This interface specifies two very important methods, init() and service(). The init() method is called only once by the servlet engine - when the servlet instance is created - while the service() method is executed every time a request to a servlet is made. This means that, depending on the servlet engine, multiple threads can execute the service() method at the same time, which is important if you have servlet member variables that need to be thread-safe. The Servlet API provides simple implementations of the Servlet interface in the form of the GenericServlet and HttpServlet classes. The e-mail servlet described here extends the HttpServlet class and overrides its init(), doPost() and doGet() methods.
JavaMail
The JavaMail API provides a platform-independent and protocol-independent framework to build mail and messaging applications. The JavaMail API is a part of the J2EE. The e-mail servlet described here has been developed and tested with JavaMail version 1.1.3. The JavaMail API includes implementations of the IMAP and SMTP service providers. The POP3 service provider is available for download as a separate set of APIs. Almost 100 interfaces and classes are provided with the JavaMail API. The main ones to note are the Session, Store, Folder, Address, Message and BodyPart classes.
Three-Tier Thin Client Web Architecture
Multitier Web architectures have been widely adopted by today's enterprises. The Web e-mail solution described here is a three-tier application with a presentation tier, a business logic tier and a persistent storage tier (see Figure 1). The presentation tier is a Java servlet that processes user requests and generates HTML pages. The business logic tier uses JavaMail to validate users; track sessions; retrieve, compose and delete messages; and handle attachments. The persistency tier can be any e-mail server that supports SMTP and POP3 or IMAP.
Initialization
The Servlet interface specifies that each implementation must have the init() method. The host servlet engine calls the init() method just once and must complete successfully before the servlet can receive any requests. In this case the init() method retrieves configuration parameters and initializes the default mail session. The parameters I used to develop and test the servlet are in Listing 1. The Java source for the init() method is in Listing 2. To successfully send and receive e-mail, the servlet needs the IP addresses of the incoming and outgoing mail servers. In most cases these addresses are the same, but there are cases when the servers might be separated. Next, the incoming mail protocol parameter needs to be assigned to the class variable, thus avoiding the parameter lookup every time a user logs on. JavaMail supports IMAP by default. The POP3 protocol is also supported, but has to be downloaded from JavaSoft as a separate product.






