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:
|

|
Configuring your web server to talk
WAP
To configure your web server to work with
the WAP MIME types you must add the WAP MIME types to the web server's
configuration files, as was discussed in Chapter 2.
Java Activation Framework (JAF)
The JavaMail API leverages the capabilities
for dealing with complex data types from the Java Activation Framework (JAF),
which is part of the Glasgow
JavaBeans specification. JAF provides Java with similar capabilities that
plug-ins provide for web browsers. The Java Activation Framework allows for the
querying and handling of multi-media data types, although this is of limited
use on the current generation of WAP-enabled phones.
As more advanced and richer featured
devices become available, JAF will provide the programming framework needed to
integrate our e-mail application with the functionality of MIME. Enhancing the
next generation of wireless devices to utilize complex mail attachments will be
a trivial step using JAF, but will be a critical step in making WAP devices a must have commodity item.
The most important JavaMail classes
We will now introduce some of the most important JavaMail classes that we will see in used in
later examples.
javax.mail.Session
The javax.mail.Session class
(not to be confused with the javax.servlet.http.HttpSession class) is used to control access to the implementations of the
other mail classes that represent the services offered by the mail system, for
instance javax.mail.Store.
javax.mail.Transport
This class is used for sending mail
messages via a specific protocol such as SMTP, as implemented by the service
provider.
javax.mail.Store
This class is implemented by the service
provider. It aims to allow access to read, write, monitor, and search
activities for a particular mail protocol such as POP3, or IMAP4. A reference
to the javax.mail.Folder class is obtained via this class.
javax.mail.Folder
The javax.mail.Folder class
gives a hierarchical view of javax.mail.Message objects, and provides access to specific messages for read, delete
and reply actions.
javax.mail.internet.MimeMessage
This class models the actual mail message.
The javax.mail.internet.MimeMessage class holds very little information and data about the message when
it is first instantiated; as successive methods retrieve more data about the
message, this class is used to store that data.
This lightweight
message structure – populating the message with data only as it is needed – is
an advantage when you wish to scroll through lists of message headers in your
inbox without downloading the whole of each item. If a message contains a 10Mb
attachment, this need only be retrieved when it is required, which considerably
increases the speed of viewing lists of messages on a first generation GSM WAP
phone!
The ability to download the attributes of a message, and not its entire contents, is optional for compliance with the POP3 protocol. However, most POP3 implementations allow for this partial retrieval of message information, enabling the JavaMail implementation to take advantage of
this feature.
javax.mail.internet.InternetAddress
This class
models a RFC822 e-mail address, i.e. an address of the form john_doe@wapbook.org. If an incorrect address format is encountered an error occurs, and
an AddressException is thrown, within the Java method processing the e-mail address.
We will now take a quick look at a class designed to make the generation of WML easier for the servlet to handle. We can do this by providing a class that models the creation of WML, abstracting out some routine tasks such as generating the WML header, and opening, and closing both cards and the deck itself.


©1999 Wrox Press Limited, US and UK.