Introduction to the JavaMail API
The JavaMail API offers a clean object-oriented framework of classes that model a theoretical mail system. JavaMail is platform-independent and protocol-independent, and
therefore presents an ideal way to build e-mail and messaging solutions that will work with WAP technology.
Many applications can benefit from e-mail support; using JavaMail, developers can rapidly construct messaging functionality whilst abstracting the underlying vendor's
implementation in a similar way to the abstraction achieved from relational databases with JDBC.
In many ways the JavaMail service providers act in a similar way to those that provide JDBC drivers, doing for SMTP, Lotus Notes etc, what JDBC does for Oracle and Sybase. In our examples we use the SMTP and POP3 drivers provided by Sun.
JavaMail models an abstract messaging service and any vendor-specific mail system that provides a JavaMail interface implementation can be accessed with minimal (ideally no) recoding.
Next we are going to take a look at some examples that make use of e-mail capabilities using JavaMail. The first example will show how an m-commerce application can use SMTP to send an e-mail confirming to the customers that their order has been placed, and the second example will utilize SMTP and POP3 to code a WAP based web mail application.
The following sections assume a basic working knowledge of Java Servlets. These were introduced in Chapter 10, but for more detailed information, see Java Server Programming by Andrew Patzer et al, published by Wrox.ISBN 1861002777.
Installation of tools
All the examples in this chapter were tested under Microsoft Windows NT version 4.0, service pack 5, and Windows 98 and Windows 2000. You will need to download and install the following software to compile and run the examples in the rest of this chapter:
- JDK (Java Development Kit) 1.2.2. Download from http://java.sun.com/jdk/
- JSDK (Java Servlet Development Kit) 2.1. Download from http://java.sun.com/products/ (This is also available in the JSWDK download)
- JAF (Java Activation Framework). Download from http://java.sun.com/beans/glasgow/jaf.html
- Java Mail 1.1.3, which includes an SMTP service provider. Download from http://java.sun.com/products/javamail
- Java Mail POP3 provider. Separate download from http://java.sun.com/products/javamail/pop3.html
You will also need to have access to a web server, in which to deploy the Java servlets we will write, and a WAP browser emulator, to test the code. In this chapter we used:
- Gefion Web Server Lite (http://www.gefionsoftware.com/LiteWebServer/)
- Phone.com UP.SDK 4.0 beta 2 Simulator (http://www.phone.com)
Access to a SMTP server and a POP3 server is also needed. You can find out your POP3 and SMTP host names by contacting your Internet Service Provider or network administrator. They will be similar to the following:
- pop.yourISP.net
- smtp.yourISP.net
Classpath
You will need to add the following jar files to your Java environment's CLASSPATH variable in order to compile the example.
Under Windows 95/98 the relevant CLASSPATH amendments can be made in your autoexec.bat:
CLASSPATH = C:\jaf-1.0.1\activation.jar;
CLASSPATH = %CLASSPATH%;c:\jsdk2.1\servlet.jar;
CLASSPATH = %CLASSPATH%;c:\pop3-1.1.1\pop3.jar;
CLASSPATH = %CLASSPATH%;c:\javamail-1.1.3\mail.jar;
CLASSPATH = %CLASSPATH%;c:\jdk1.2.2\lib\WML.jar;
Under Windows NT, you can make the necessary changes by right-mouse clicking on the My Computer icon on your desktop and choosing Properties, or opening the System icon in the Control Panel. The Classpath environment variable can then be added or modified in the Environment tab.