364N/A * Copyright (c) 1998-2011 Oracle and/or its affiliates. All rights reserved. 0N/A * Redistribution and use in source and binary forms, with or without 0N/A * modification, are permitted provided that the following conditions 0N/A * - Redistributions of source code must retain the above copyright 0N/A * notice, this list of conditions and the following disclaimer. 0N/A * - Redistributions in binary form must reproduce the above copyright 0N/A * notice, this list of conditions and the following disclaimer in the 0N/A * documentation and/or other materials provided with the distribution. 292N/A * - Neither the name of Oracle nor the names of its 0N/A * contributors may be used to endorse or promote products derived 0N/A * from this software without specific prior written permission. 0N/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 0N/A * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 0N/A * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 0N/A * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 0N/A * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 0N/A * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 0N/A * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 0N/A * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 0N/A * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 0N/A * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 0N/A * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 0N/A * This is a servlet that demonstrates the use of JavaMail APIs 0N/A * in a 3-tier application. It allows the user to login to an 0N/A * IMAP store, list all the messages in the INBOX folder, view 0N/A * selected messages, compose and send a message, and logout. 0N/A * Please note: This is NOT an example of how to write servlets! 0N/A * This is simply to show that JavaMail can be used in a servlet. 0N/A * For more information on this servlet, see the 0N/A * For more information on servlets, see 0N/A * @author Max Spivak 0N/A * This method handles the "POST" submission from two forms: the 0N/A * login form and the message compose form. The login form has the 0N/A * following parameters: <code>hostname</code>, <code>username</code>, 0N/A * and <code>password</code>. The <code>send</code> parameter denotes 0N/A * that the method is processing the compose form submission. 0N/A // process message sending 0N/A // track who logged in 0N/A // save stuff into MUD 0N/A out.
print(
"<font face=\"Arial,Helvetica\" font size=+3>");
0N/A // folder name column header 0N/A out.
print(
"<tr><td width=\"75%\" bgcolor=\"#ffffcc\">");
0N/A out.
print(
"<font face=\"Arial,Helvetica\" font size=-1>");
0N/A // msg count column header 0N/A out.
print(
"<font face=\"Arial,Helvetica\" font size=-1>");
0N/A out.
print(
"<tr><td width=\"75%\" bgcolor=\"#ffffff\">");
0N/A "Inbox" +
"</a></td><br>");
0N/A * This method handles the GET requests for the client. 0N/A out.
println(
"<html><body>Please Login (no session)</body></html>");
0N/A // mux that takes a GET request, based on parameters figures 0N/A // out what it should do, and routes it to the 0N/A // appropriate method 0N/A // get url parameters 0N/A // process url params 0N/A // operate on message "msgStr" 0N/A // display message "msgStr" 0N/A // display part "part" in message "msgStr" 0N/A // display compose form 0N/A /* main method to display messages */ 0N/A "/INBOX</b></font></center><p>");
0N/A // first, display this message's headers 0N/A // and now, handle the content 0N/A //if (o instanceof String) { 0N/A //} else if (o instanceof Multipart){ 0N/A for (
int i =
0; i <
cnt; i++) {
0N/A * This method displays a message part. <code>text/plain</code> 0N/A * content parts are displayed inline. For all other parts, 0N/A * a URL is generated and displayed; clicking on the URL 0N/A * brings up the part in a separate page. 0N/A // generate a url for this part 0N/A * This method gets the stream from for a given msg part and 0N/A * pushes it out to the browser with the correct content type. 0N/A * Used to display attachments and relies on the browser's 0N/A * content handling capabilities. 0N/A * This is a utility message that pretty-prints the message 0N/A * headers for message that is being displayed. 0N/A * This method displays the URL's for the available commands and the 0N/A out.
print(
"<center><font face=\"Arial,Helvetica\" font size=\"+3\">");
0N/A "/INBOX</b></font></center><p>");
0N/A // URL's for the commands that are available 0N/A out.
println(
"<font face=\"Arial,Helvetica\" font size=\"+3\"><b>");
0N/A "?logout=true\">Logout</a>");
0N/A "?compose=true\" target=\"compose\">Compose</a>");
0N/A // List headers in a table 0N/A // sender column header 0N/A // date column header 0N/A // subject column header 0N/A // for each message, show its headers 0N/A // if message has the DELETED flag set, don't display it 29N/A "<i>No Subject</i>") +
0N/A * This method handles the request when the user hits the 0N/A * <i>Compose</i> link. It send the compose form to the browser. 0N/A * This method processes the send request from the compose form 0N/A // utility method; returns a string suitable for msg header display 0N/A // utility method; retrieve the MailUserData 0N/A // from the HttpSession and return it 29N/A return "A mail reader servlet";
0N/A * This is the HTML code for the compose form. Another option would 0N/A * have been to use a separate html page. 0N/A 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%\"> </TD><TD WIDTH=\"84%\"><TEXTAREA NAME=\"text\" ROWS=\"15\" COLS=\"53\"></TEXTAREA></TD></TR><TR><TD WIDTH=\"16%\" HEIGHT=\"32\"> </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>";
0N/A * This class is used to store session data for each user's session. It 0N/A * is stored in the HttpSession.