Subversion Repositories javautils

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Copyright 1998, 1999 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  *
  8.  *   - Redistributions of source code must retain the above copyright
  9.  *     notice, this list of conditions and the following disclaimer.
  10.  *
  11.  *   - Redistributions in binary form must reproduce the above copyright
  12.  *     notice, this list of conditions and the following disclaimer in the
  13.  *     documentation and/or other materials provided with the distribution.
  14.  *
  15.  *   - Neither the name of Sun Microsystems nor the names of its
  16.  *     contributors may be used to endorse or promote products derived
  17.  *     from this software without specific prior written permission.
  18.  *
  19.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  20.  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  21.  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  22.  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  23.  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  25.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  26.  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  27.  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28.  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30.  */
  31.  
  32. import java.io.*;
  33. import java.util.*;
  34. import java.text.*;
  35.  
  36. import javax.servlet.*;
  37. import javax.servlet.http.*;
  38. import javax.mail.*;
  39. import javax.mail.internet.*;
  40. import javax.activation.*;
  41.  
  42.  
  43. /**
  44.  * This is a servlet that demonstrates the use of JavaMail APIs
  45.  * in a 3-tier application. It allows the user to login to an
  46.  * IMAP store, list all the messages in the INBOX folder, view
  47.  * selected messages, compose and send a message, and logout.
  48.  * <p>
  49.  * Please note: This is NOT an example of how to write servlets!
  50.  * This is simply to show that JavaMail can be used in a servlet.
  51.  * <p>
  52.  * For more information on this servlet, see the
  53.  * JavaMailServlet.README.txt file.
  54.  * <p>
  55.  * For more information on servlets, see
  56.  * <a href="http://java.sun.com/products/java-server/servlets/index.html">
  57.  * http://java.sun.com/products/java-server/servlets/index.html</a>
  58.  *
  59.  * @author Max Spivak
  60.  */
  61. public class JavaMailServlet extends HttpServlet implements SingleThreadModel {
  62.     String protocol = "imap";
  63.     String mbox = "INBOX";
  64.  
  65.  
  66.     /**
  67.      * This method handles the "POST" submission from two forms: the
  68.      * login form and the message compose form. The login form has the
  69.      * following parameters: <code>hostname</code>, <code>username</code>,
  70.      * and <code>password</code>. The <code>send</code> parameter denotes
  71.      * that the method is processing the compose form submission.
  72.      */
  73.     public  void doPost(HttpServletRequest req, HttpServletResponse res)
  74.         throws ServletException, IOException {
  75.  
  76.         // get the session
  77.         HttpSession ssn = req.getSession(true);
  78.  
  79.         String send = req.getParameter("send");
  80.         String host = req.getParameter("hostname");
  81.         String user = req.getParameter("username");
  82.         String passwd = req.getParameter("password");
  83.         URLName url = new URLName(protocol, host, -1, mbox, user, passwd);
  84.  
  85.         ServletOutputStream out = res.getOutputStream();
  86.         res.setContentType("text/html");
  87.         out.println("<html><body bgcolor=\"#CCCCFF\">");
  88.  
  89.         if (send != null) {
  90.             // process message sending
  91.             send(req, res, out, ssn);
  92.  
  93.         } else {
  94.             // initial login
  95.  
  96.             // create
  97.             MailUserData mud = new MailUserData(url);
  98.             ssn.putValue("javamailservlet", mud);
  99.            
  100.             try {
  101.                 Properties props = System.getProperties();
  102.                 props.put("mail.smtp.host", host);
  103.                 Session session = Session.getDefaultInstance(props, null);
  104.                 session.setDebug(false);
  105.                 Store store = session.getStore(url);
  106.                 store.connect();
  107.                 Folder folder = store.getDefaultFolder();
  108.                 if (folder == null)
  109.                     throw new MessagingException("No default folder");
  110.                
  111.                 folder = folder.getFolder(mbox);
  112.                 if (folder == null)
  113.                     throw new MessagingException("Invalid folder");
  114.                
  115.                 folder.open(Folder.READ_WRITE);
  116.                 int totalMessages = folder.getMessageCount();
  117.                 Message[] msgs = folder.getMessages();
  118.                 FetchProfile fp = new FetchProfile();
  119.                 fp.add(FetchProfile.Item.ENVELOPE);
  120.                 folder.fetch(msgs, fp);
  121.                
  122.                 // track who logged in
  123.                 System.out.println("Login from: " + store.getURLName());
  124.                
  125.                 // save stuff into MUD
  126.                 mud.setSession(session);
  127.                 mud.setStore(store);
  128.                 mud.setFolder(folder);
  129.                
  130.                 // splash
  131.                 out.print("<center>");
  132.                 out.print("<font face=\"Arial,Helvetica\" font size=+3>");
  133.                 out.println("<b>Welcome to JavaMail!</b></font></center><p>");
  134.  
  135.                 // folder table
  136.                 out.println("<table width=\"50%\" border=0 align=center>");
  137.                 // folder name column header
  138.                 out.print("<tr><td width=\"75%\" bgcolor=\"#ffffcc\">");
  139.                 out.print("<font face=\"Arial,Helvetica\" font size=-1>");
  140.                 out.println("<b>FolderName</b></font></td><br>");
  141.                 // msg count column header
  142.                 out.print("<td width=\"25%\" bgcolor=\"#ffffcc\">");
  143.                 out.print("<font face=\"Arial,Helvetica\" font size=-1>");
  144.                 out.println("<b>Messages</b></font></td><br>");
  145.                 out.println("</tr>");
  146.                 // folder name
  147.                 out.print("<tr><td width=\"75%\" bgcolor=\"#ffffff\">");
  148.                 out.print("<a href=\"" + HttpUtils.getRequestURL(req) + "\">" +
  149.                           "Inbox" + "</a></td><br>");
  150.                 // msg count
  151.                 out.println("<td width=\"25%\" bgcolor=\"#ffffff\">" +
  152.                             totalMessages + "</td>");
  153.                 out.println("</tr>");
  154.                 out.println("</table");
  155.             } catch (Exception ex) {
  156.                 out.println(ex.toString());            
  157.             } finally {
  158.                 out.println("</body></html>");
  159.                 out.close();
  160.             }
  161.         }
  162.     }
  163.  
  164.  
  165.     /**
  166.      * This method handles the GET requests for the client.
  167.      */
  168.     public void doGet (HttpServletRequest req, HttpServletResponse res)
  169.         throws ServletException, IOException {
  170.  
  171.         HttpSession ses = req.getSession(false); // before we write to out
  172.         ServletOutputStream out = res.getOutputStream();
  173.         MailUserData mud = getMUD(ses);
  174.  
  175.         if (mud == null) {
  176.             res.setContentType("text/html");
  177.             out.println("<html><body>Please Login (no session)</body></html>");
  178.             out.close();
  179.             return;
  180.         }
  181.  
  182.         if (!mud.getStore().isConnected()) {
  183.             res.setContentType("text/html");
  184.             out.println("<html><body>Not Connected To Store</body></html>");
  185.             out.close();
  186.             return;
  187.         }
  188.  
  189.  
  190.         // mux that takes a GET request, based on parameters figures
  191.         // out what it should do, and routes it to the
  192.         // appropriate method
  193.  
  194.         // get url parameters
  195.         String msgStr = req.getParameter("message");
  196.         String logout = req.getParameter("logout");
  197.         String compose = req.getParameter("compose");
  198.         String part = req.getParameter("part");
  199.         int msgNum = -1;
  200.         int partNum = -1;
  201.  
  202.         // process url params
  203.         if (msgStr != null) {
  204.             // operate on message "msgStr"
  205.             msgNum = Integer.parseInt(msgStr);
  206.  
  207.             if (part == null) {
  208.                 // display message "msgStr"
  209.                 res.setContentType("text/html");
  210.                 displayMessage(mud, req, out, msgNum);
  211.  
  212.             } else if (part != null) {
  213.                 // display part "part" in message "msgStr"
  214.                 partNum = Integer.parseInt(part);
  215.                 displayPart(mud, msgNum, partNum, out, res);
  216.             }
  217.  
  218.         } else if (compose != null) {
  219.             // display compose form
  220.             compose(mud, res, out);
  221.  
  222.         } else if (logout != null) {
  223.             // process logout
  224.             try {
  225.                 mud.getFolder().close(false);
  226.                 mud.getStore().close();
  227.                 ses.invalidate();
  228.                 out.println("<html><body>Logged out OK</body></html>");
  229.             } catch (MessagingException mex) {
  230.                 out.println(mex.toString());
  231.             }
  232.  
  233.         } else {
  234.             // display headers
  235.             displayHeaders(mud, req, out);
  236.         }
  237.     }
  238.  
  239.     /* main method to display messages */
  240.     private void displayMessage(MailUserData mud, HttpServletRequest req,
  241.                                 ServletOutputStream out, int msgNum)
  242.         throws IOException {
  243.            
  244.         out.println("<html>");
  245.         out.println("<HEAD><TITLE>JavaMail Servlet</TITLE></HEAD>");
  246.         out.println("<BODY bgcolor=\"#ccccff\">");
  247.         out.print("<center><font face=\"Arial,Helvetica\" ");
  248.         out.println("font size=\"+3\"><b>");
  249.         out.println("Message " + (msgNum+1) + " in folder " +
  250.                     mud.getStore().getURLName() +
  251.                     "/INBOX</b></font></center><p>");
  252.  
  253.         try {
  254.             Message msg = mud.getFolder().getMessage(msgNum);
  255.  
  256.             // first, display this message's headers
  257.             displayMessageHeaders(mud, msg, out);
  258.  
  259.             // and now, handle the content
  260.             Object o = msg.getContent();
  261.            
  262.             //if (o instanceof String) {
  263.             if (msg.isMimeType("text/plain")) {
  264.                 out.println("<pre>");
  265.                 out.println((String)o);
  266.                 out.println("</pre>");
  267.             //} else if (o instanceof Multipart){
  268.             } else if (msg.isMimeType("multipart/*")) {
  269.                 Multipart mp = (Multipart)o;
  270.                 int cnt = mp.getCount();
  271.                 for (int i = 0; i < cnt; i++) {
  272.                     displayPart(mud, msgNum, mp.getBodyPart(i), i, req, out);
  273.                 }
  274.             } else {
  275.                 out.println(msg.getContentType());
  276.             }
  277.  
  278.         } catch (MessagingException mex) {
  279.             out.println(mex.toString());
  280.         }
  281.  
  282.         out.println("</BODY></html>");
  283.         out.close();
  284.     }
  285.  
  286.     /**
  287.      * This method displays a message part. <code>text/plain</code>
  288.      * content parts are displayed inline. For all other parts,
  289.      * a URL is generated and displayed; clicking on the URL
  290.      * brings up the part in a separate page.
  291.      */
  292.     private void displayPart(MailUserData mud, int msgNum, Part part,
  293.                              int partNum, HttpServletRequest req,
  294.                              ServletOutputStream out)
  295.         throws IOException {
  296.  
  297.         if (partNum != 0)
  298.             out.println("<p><hr>");
  299.  
  300.         try {
  301.  
  302.             String sct = part.getContentType();
  303.             if (sct == null) {
  304.                 out.println("invalid part");
  305.                 return;
  306.             }
  307.             ContentType ct = new ContentType(sct);
  308.            
  309.             if (partNum != 0)
  310.                 out.println("<b>Attachment Type:</b> " +  
  311.                             ct.getBaseType() + "<br>");
  312.  
  313.             if (ct.match("text/plain")) {  
  314.                 // display text/plain inline
  315.                 out.println("<pre>");
  316.                 out.println((String)part.getContent());
  317.                 out.println("</pre>");
  318.  
  319.             } else {
  320.                 // generate a url for this part
  321.                 String s;
  322.                 if ((s = part.getFileName()) != null)
  323.                     out.println("<b>Filename:</b> " + s + "<br>");
  324.                 s = null;
  325.                 if ((s = part.getDescription()) != null)
  326.                     out.println("<b>Description:</b> " + s + "<br>");
  327.                
  328.                 out.println("<a href=\"" +
  329.                             HttpUtils.getRequestURL(req) +
  330.                             "?message=" +
  331.                             msgNum + "&part=" +
  332.                             partNum + "\">Display Attachment</a>");
  333.             }
  334.         } catch (MessagingException mex) {
  335.             out.println(mex.toString());
  336.         }
  337.     }
  338.  
  339.     /**
  340.      * This method gets the stream from for a given msg part and
  341.      * pushes it out to the browser with the correct content type.
  342.      * Used to display attachments and relies on the browser's
  343.      * content handling capabilities.
  344.      */
  345.     private void displayPart(MailUserData mud, int msgNum,
  346.                              int partNum, ServletOutputStream out,
  347.                              HttpServletResponse res)
  348.         throws IOException {
  349.  
  350.         Part part = null;
  351.        
  352.         try {
  353.             Message msg = mud.getFolder().getMessage(msgNum);
  354.  
  355.             Multipart mp = (Multipart)msg.getContent();
  356.             part = mp.getBodyPart(partNum);
  357.            
  358.             String sct = part.getContentType();
  359.             if (sct == null) {
  360.                 out.println("invalid part");
  361.                 return;
  362.             }
  363.             ContentType ct = new ContentType(sct);
  364.  
  365.             res.setContentType(ct.getBaseType());
  366.             InputStream is = part.getInputStream();
  367.             int i;
  368.             while ((i = is.read()) != -1)
  369.                 out.write(i);
  370.             out.flush();
  371.             out.close();
  372.         } catch (MessagingException mex) {
  373.             out.println(mex.toString());
  374.         }
  375.     }
  376.  
  377.     /**
  378.      * This is a utility message that pretty-prints the message
  379.      * headers for message that is being displayed.
  380.      */
  381.     private void displayMessageHeaders(MailUserData mud, Message msg,
  382.                                        ServletOutputStream out)
  383.         throws IOException {
  384.  
  385.         try {
  386.             out.println("<b>Date:</b> " + msg.getSentDate() + "<br>");
  387.  
  388.             Address[] fr = msg.getFrom();
  389.             if (fr != null) {
  390.                 boolean tf = true;
  391.                 out.print("<b>From:</b> ");
  392.                 for (int i = 0; i < fr.length; i++) {
  393.                     out.print(((tf) ? " " : ", ") + getDisplayAddress(fr[i]));
  394.                     tf = false;
  395.                 }
  396.                 out.println("<br>");
  397.             }
  398.  
  399.             Address[] to = msg.getRecipients(Message.RecipientType.TO);
  400.             if (to != null) {
  401.                 boolean tf = true;
  402.                 out.print("<b>To:</b> ");
  403.                 for (int i = 0; i < to.length; i++) {
  404.                     out.print(((tf) ? " " : ", ") + getDisplayAddress(to[i]));
  405.                     tf = false;
  406.                 }
  407.                 out.println("<br>");
  408.             }
  409.  
  410.             Address[] cc = msg.getRecipients(Message.RecipientType.CC);
  411.             if (cc != null) {
  412.                 boolean cf = true;
  413.                 out.print("<b>CC:</b> ");
  414.                 for (int i = 0; i < cc.length; i++) {
  415.                     out.print(((cf) ? " " : ", ") + getDisplayAddress(cc[i]));
  416.                     cf = false;
  417.                 }
  418.                 out.println("<br>");
  419.             }
  420.            
  421.             out.print("<b>Subject:</b> " +
  422.                       ((msg.getSubject() !=null) ? msg.getSubject() : "") +
  423.                       "<br>");
  424.  
  425.         } catch (MessagingException mex) {
  426.             out.println(msg.toString());
  427.         }
  428.     }
  429.  
  430.     /**
  431.      * This method displays the URL's for the available commands and the
  432.      * INBOX headerlist
  433.      */
  434.     private void displayHeaders(MailUserData mud,
  435.                                 HttpServletRequest req,
  436.                                 ServletOutputStream out)
  437.         throws IOException {
  438.  
  439.         SimpleDateFormat df = new SimpleDateFormat("EE M/d/yy");
  440.  
  441.         out.println("<html>");
  442.         out.println("<HEAD><TITLE>JavaMail Servlet</TITLE></HEAD>");
  443.         out.println("<BODY bgcolor=\"#ccccff\"><hr>");
  444.         out.print("<center><font face=\"Arial,Helvetica\" font size=\"+3\">");
  445.         out.println("<b>Folder " + mud.getStore().getURLName() +
  446.                     "/INBOX</b></font></center><p>");
  447.  
  448.         // URL's for the commands that are available
  449.         out.println("<font face=\"Arial,Helvetica\" font size=\"+3\"><b>");
  450.         out.println("<a href=\"" +
  451.                     HttpUtils.getRequestURL(req) +
  452.                     "?logout=true\">Logout</a>");
  453.         out.println("<a href=\"" +
  454.                     HttpUtils.getRequestURL(req) +
  455.                     "?compose=true\" target=\"compose\">Compose</a>");
  456.         out.println("</b></font>");
  457.         out.println("<hr>");
  458.  
  459.         // List headers in a table
  460.         out.print("<table cellpadding=1 cellspacing=1 "); // table
  461.         out.println("width=\"100%\" border=1>");          // settings
  462.  
  463.         // sender column header
  464.         out.println("<tr><td width=\"25%\" bgcolor=\"ffffcc\">");
  465.         out.println("<font face=\"Arial,Helvetica\" font size=\"+1\">");
  466.         out.println("<b>Sender</b></font></td>");
  467.         // date column header
  468.         out.println("<td width=\"15%\" bgcolor=\"ffffcc\">");
  469.         out.println("<font face=\"Arial,Helvetica\" font size=\"+1\">");
  470.         out.println("<b>Date</b></font></td>");
  471.         // subject column header
  472.         out.println("<td bgcolor=\"ffffcc\">");
  473.         out.println("<font face=\"Arial,Helvetica\" font size=\"+1\">");
  474.         out.println("<b>Subject</b></font></td></tr>");
  475.  
  476.         try {
  477.             Folder f = mud.getFolder();
  478.             int msgCount = f.getMessageCount();
  479.             Message m = null;
  480.             // for each message, show its headers
  481.             for (int i = 1; i <= msgCount; i++) {
  482.                 m = f.getMessage(i);
  483.                
  484.                 // if message has the DELETED flag set, don't display it
  485.                 if (m.isSet(Flags.Flag.DELETED))
  486.                     continue;
  487.  
  488.                 // from
  489.                 out.println("<tr valigh=middle>");
  490.                 out.print("<td width=\"25%\" bgcolor=\"ffffff\">");
  491.                 out.println("<font face=\"Arial,Helvetica\">" +
  492.                             ((m.getFrom() != null) ?
  493.                                        m.getFrom()[0].toString() :
  494.                                        "" ) +
  495.                             "</font></td>");
  496.  
  497.                 // date
  498.                 out.print("<td nowrap width=\"15%\" bgcolor=\"ffffff\">");
  499.                 out.println("<font face=\"Arial,Helvetica\">" +
  500.                             df.format((m.getSentDate()!=null) ?
  501.                                       m.getSentDate() : m.getReceivedDate()) +
  502.                             "</font></td>");
  503.  
  504.                 // subject & link
  505.                 out.print("<td bgcolor=\"ffffff\">");
  506.                 out.println("<font face=\"Arial,Helvetica\">" +
  507.                             "<a href=\"" +
  508.                             HttpUtils.getRequestURL(req) +
  509.                             "?message=" +
  510.                             i + "\">" +
  511.                             ((m.getSubject() != null) ?
  512.                                    m.getSubject() :
  513.                                    "<i>No Subject</i>") +
  514.                             "</a>" +
  515.                             "</font></td>");
  516.                 out.println("</tr>");
  517.             }
  518.         } catch (MessagingException mex) {
  519.             out.println("<tr><td>" + mex.toString() + "</td></tr>");
  520.             mex.printStackTrace();
  521.         }
  522.  
  523.         out.println("</table>");
  524.         out.println("</BODY></html>");
  525.         out.flush();
  526.         out.close();
  527.     }
  528.  
  529.     /**
  530.      * This method handles the request when the user hits the
  531.      * <i>Compose</i> link. It send the compose form to the browser.
  532.      */
  533.     private void compose(MailUserData mud, HttpServletResponse res,
  534.                          ServletOutputStream out)
  535.         throws IOException {
  536.        
  537.         res.setContentType("text/html");
  538.         out.println(composeForm);
  539.         out.close();
  540.     }
  541.  
  542.     /**
  543.      * This method processes the send request from the compose form
  544.      */
  545.     private void send(HttpServletRequest req, HttpServletResponse res,
  546.                       ServletOutputStream out, HttpSession ssn)
  547.         throws IOException {
  548.            
  549.         String to = req.getParameter("to");
  550.         String cc = req.getParameter("cc");
  551.         String subj = req.getParameter("subject");
  552.         String text = req.getParameter("text");
  553.  
  554.         try {
  555.             MailUserData mud = getMUD(ssn);
  556.             if (mud == null)
  557.                 throw new Exception("trying to send, but not logged in");
  558.  
  559.             Message msg = new MimeMessage(mud.getSession());
  560.             InternetAddress[] toAddrs = null, ccAddrs = null;
  561.  
  562.             if (to != null) {
  563.                 toAddrs = InternetAddress.parse(to, false);
  564.                 msg.setRecipients(Message.RecipientType.TO, toAddrs);
  565.             } else
  566.                 throw new MessagingException("No \"To\" address specified");
  567.  
  568.             if (cc != null) {
  569.                 ccAddrs = InternetAddress.parse(cc, false);
  570.                 msg.setRecipients(Message.RecipientType.CC, ccAddrs);
  571.             }
  572.  
  573.             if (subj != null)
  574.                 msg.setSubject(subj);
  575.  
  576.             URLName u = mud.getURLName();
  577.             msg.setFrom(new InternetAddress(u.getUsername() + "@" +
  578.                                             u.getHost()));
  579.  
  580.             if (text != null)
  581.                 msg.setText(text);
  582.  
  583.             Transport.send(msg);
  584.            
  585.             out.println("<h1>Message sent successfully</h1></body></html>");
  586.             out.close();
  587.            
  588.         } catch (Exception mex) {
  589.             out.println("<h1>Error sending message.</h1>");
  590.             out.println(mex.toString());
  591.             out.println("<br></body></html>");
  592.         }
  593.     }
  594.  
  595.  
  596.     // utility method; returns a string suitable for msg header display
  597.     private String getDisplayAddress(Address a) {
  598.         String pers = null;
  599.         String addr = null;
  600.         if (a instanceof InternetAddress &&
  601.             ((pers = ((InternetAddress)a).getPersonal()) != null)) {
  602.            
  603.             addr = pers + "  "+"&lt;"+((InternetAddress)a).getAddress()+"&gt;";
  604.         } else
  605.             addr = a.toString();
  606.        
  607.         return addr;
  608.     }
  609.  
  610.     // utility method; retrieve the MailUserData
  611.     // from the HttpSession and return it
  612.     private MailUserData getMUD(HttpSession ses) throws IOException {
  613.         MailUserData mud = null;
  614.  
  615.         if (ses == null) {
  616.             return null;
  617.         } else {
  618.             if ((mud = (MailUserData)ses.getValue("javamailservlet")) == null){
  619.                 return null;
  620.             }
  621.         }
  622.         return mud;
  623.     }
  624.  
  625.  
  626.     public String getServletInfo() {
  627.         return "A mail reader servlet";
  628.     }
  629.  
  630.     /**
  631.      * This is the HTML code for the compose form. Another option would
  632.      * have been to use a separate html page.
  633.      */
  634.     private static String composeForm = "<HTML><HEAD><TITLE>JavaMail Compose</TITLE></HEAD><BODY BGCOLOR=\"#CCCCFF\"><FORM ACTION=\"/servlet/JavaMailServlet\" METHOD=\"POST\"><input type=\"hidden\" name=\"send\" value=\"send\"><P ALIGN=\"CENTER\"><B><FONT SIZE=\"4\" FACE=\"Verdana, Arial, Helvetica\">JavaMail Compose Message</FONT></B><P><TABLE BORDER=\"0\" WIDTH=\"100%\"><TR><TD WIDTH=\"16%\" HEIGHT=\"22\">     <P ALIGN=\"RIGHT\"><B><FONT FACE=\"Verdana, Arial, Helvetica\">To:</FONT></B></TD><TD WIDTH=\"84%\" HEIGHT=\"22\"><INPUT TYPE=\"TEXT\" NAME=\"to\" SIZE=\"30\"> <FONT SIZE=\"1\" FACE=\"Verdana, Arial, Helvetica\"> (separate addresses with commas)</FONT></TD></TR><TR><TD WIDTH=\"16%\"><P ALIGN=\"RIGHT\"><B><FONT FACE=\"Verdana, Arial, Helvetica\">CC:</FONT></B></TD><TD WIDTH=\"84%\"><INPUT TYPE=\"TEXT\" NAME=\"cc\" SIZE=\"30\"> <FONT SIZE=\"1\" FACE=\"Verdana, Arial, Helvetica\"> (separate addresses with commas)</FONT></TD></TR><TR><TD WIDTH=\"16%\"><P ALIGN=\"RIGHT\"><B><FONT FACE=\"Verdana, Arial, Helvetica\">Subject:</FONT></B></TD><TD WIDTH=\"84%\"><INPUT TYPE=\"TEXT\" NAME=\"subject\" SIZE=\"55\"></TD></TR><TR><TD WIDTH=\"16%\">&nbsp;</TD><TD WIDTH=\"84%\"><TEXTAREA NAME=\"text\" ROWS=\"15\" COLS=\"53\"></TEXTAREA></TD></TR><TR><TD WIDTH=\"16%\" HEIGHT=\"32\">&nbsp;</TD><TD WIDTH=\"84%\" HEIGHT=\"32\"><INPUT TYPE=\"SUBMIT\" NAME=\"Send\" VALUE=\"Send\"><INPUT TYPE=\"RESET\" NAME=\"Reset\" VALUE=\"Reset\"></TD></TR></TABLE></FORM></BODY></HTML>";
  635.  
  636. }
  637.  
  638.  
  639. /**
  640.  * This class is used to store session data for each user's session. It
  641.  * is stored in the HttpSession.
  642.  */
  643. class MailUserData {
  644.     URLName url;
  645.     Session session;
  646.     Store store;
  647.     Folder folder;
  648.  
  649.     public MailUserData(URLName urlname) {
  650.         url = urlname;
  651.     }
  652.  
  653.     public URLName getURLName() {
  654.         return url;
  655.     }
  656.  
  657.     public Session getSession() {
  658.         return session;
  659.     }
  660.  
  661.     public void setSession(Session s) {
  662.         session = s;
  663.     }
  664.  
  665.     public Store getStore() {
  666.         return store;
  667.     }
  668.  
  669.     public void setStore(Store s) {
  670.         store = s;
  671.     }
  672.  
  673.     public Folder getFolder() {
  674.         return folder;
  675.     }
  676.  
  677.     public void setFolder(Folder f) {
  678.         folder = f;
  679.     }
  680. }
  681.