0N/A JavaMail Servlet
0N/A ~~~~~~~~~~~~~~~~
0N/A
0N/AOverview:
0N/A=========
0N/A
0N/AJavaMailServlet should not be taken as a demo of how to use the Java
0N/AServlet API. It is rather an example of how the JavaMail APIs could
0N/Abe used in a server in a three-tier environment described by the
0N/Afollowing diagram:
0N/A
0N/A +-----------+ +-----------+ +-----------+
0N/A | IMAP | | | | |
0N/A | Server |<-IMAP->| JavaMail |<-HTTP->| WWW |
0N/A +-----------+ | Servlet |--HTML->| Browser |
0N/A | SMTP |<-SMTP->| | | |
0N/A | Server | | | | |
0N/A +-----------+ +-----------+ +-----------+
0N/A
0N/A
0N/AThe JavaMailServlet supports the following functionality:
0N/A * login to an IMAP server
0N/A * list all the messages in the INBOX folder
0N/A * view the selected message
0N/A * compose and send a message
0N/A
0N/A
0N/ASetting up and running the demo:
0N/A================================
0N/ANote: These instructions explain how to compile and run the servlet
0N/Ademo with Java Web Server (JWS). The procedure should be similar with
0N/Aother web servers that support the Java Servlet API.
0N/A
0N/A 1. Download the latest version of the Java Web Server from
0N/A http://www.sun.com/software/jwebserver/index.html and
0N/A install according to the included documentation. Make
0N/A sure JWS is listening to requests on port 80 (you may
0N/A need to modify it from default port 8080; use the JWS
0N/A Admin Applet). Make sure you can load the Java Web
0N/A Server welcome page when you connect with your browser
0N/A to the machine that the server is installed on. Also,
0N/A make sure your web browser has cookie support turned on.
0N/A
0N/A 2. Set your classpath to include the following:
0N/A * mail.jar: in the JavaMail API distribution
0N/A * activation.jar: in the JAF distribution
0N/A * jws.jar: in the /lib/ directory in JWS installation
0N/A
0N/A 3. In javamail-1.1/demo/servlet directory, compile the
0N/A JavaMailServlet.java file. That produces two class files,
0N/A JavaMailServlet.class and MailUserData.class. Copy these
0N/A class files to the /servlets/ directory in the JWS
0N/A installation.
0N/A
0N/A 4. Copy the mail.jar and activation.jar to the /lib/
0N/A directory in the JWS installation.
0N/A
0N/A 5. Copy the JavaMail.html file to the /public_html/
0N/A directory in the JWS installation.
0N/A
0N/A 6. Restart Java Web Server to pick up the new jar files
0N/A added to its lib directory. Check again that you can
0N/A load the default JWS page to verify that the server
0N/A is working fine.
0N/A
0N/A 7. Using a web browser, go to
0N/A http://<hostname>/JavaMail.html and login to a
0N/A valid IMAP account. From here on, you can view
0N/A messages in your INBOX and create and send new
0N/A messages.
0N/A
0N/A
0N/A
0N/AJavaMailServlet Design:
0N/A=======================
0N/A
0N/AThe following is a brief description of JavaMailServlet class. It
0N/Ais not intended to serve as an example of how to develop servlets;
0N/Asee http://java.sun.com/products/servlet for information on the Java
0N/AServlet API. You may find it useful to refer to JavaMailServlet.java
0N/Asource while reading this.
0N/A
0N/AThe JavaMailServlet uses two primary methods to process all
0N/Arequests: doPost() and doGet(). doPost() processes submissions
0N/Afrom the login and compose forms. When the user logs in, the
0N/AdoPost() method gets a JavaMail Session and uses the values
0N/Aof the "hostname", "username" and "password" parameters to login
0N/Ato the IMAP Store and get the INBOX Folder. To preserve state
0N/Abetween multiple HTTP requests, the necessary information
0N/A(Session, Store, Folder, URLName) are collected in the
0N/AMailUserData object which is stored using JWS's Session
0N/Atechnology (don't confuse HttpSession and JavaMail's Session--
0N/Athey are different). Finally, the doPost() method outputs
0N/Aa table listing the INBOX and the number of messages in it.
0N/A
0N/AClicking on the "INBOX" link calls the doGet() method
0N/Awhich displays a table of message headers. (See the doGet()
0N/Aand displayHeaders() methods.)
0N/A
0N/AClicking on a message generates a request to the servlet with
0N/Athe message sequence number as a parameter. The doGet() method
0N/Areceives the request and calls the displayMessage() method
0N/Apassing it the message sequence number to display. The
0N/AdisplayMessage() method first lists the message headers
0N/Aby calling the displayMessageHeaders() utility method.
0N/AFor text/plain messages, the message content is then displayed
0N/Aas a string wrapped in HTML <pre>...</pre> tags. For multipart
0N/Amessages, each part is passed to the displayPart() method.
0N/A
0N/AThere are two displayPart() methods. The one with signature
0N/A displayPart(MailUserData mud, int msgNum, Part part,
0N/A int partNum, HttpServletRequest req,
0N/A ServletOutputStream out)
0N/Ais called from the displayMessage() method for each part. For
0N/Aany part with text/plain content type, the content is output
0N/Aas a string wrapped in HTML <pre>...</pre> tags. For other
0N/Acontent types, a link representing the part is displayed,
0N/Aalong with the part filename and description, if available.
0N/A
0N/AClicking in the part link generates a request to the servlet
0N/Awith two parameters: the message sequence number and the
0N/Apart number. The doGet() method interprets the parameters and
0N/Ainvokes the second displayPart() method with the signature
0N/A displayPart(MailUserData mud, int msgNum,
0N/A int partNum, ServletOutputStream out,
0N/A HttpServletResponse res)
0N/AThis method retrieves the specified part from the message and
0N/Astreams it to the web browser, preceded by the MIME content type
0N/Aof this part. For example, if the part has a MIME type image/gif,
0N/Athe method will set the servlet response MIME content type to
0N/A"image/gif" and then follow it with the bytes of the actual
0N/Aimage. This leverages the web browser's content handling
0N/Aability to display different media types.
0N/A
0N/AMessage composition and sending is very similar to message
0N/Aviewing. When the "Compose" link is clicked in the headerlist
0N/Apage, the servlet outputs the HTML source for the compose
0N/Aform stored in the composeForm variable. The user then fills
0N/Ain the destination address, subject, text, and presses
0N/Asend. This sends a POST request to the servlet, which
0N/Ainvokes the doPost() method. doPost() calls the servlet's
0N/Asend() method, which creates a new MimeMessage and fills
0N/Ait with data retrieved from the POST request. The message
0N/Ais then sent to its destination using the Transport.send()
0N/Astatic method.