/*
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
*
* - Neither the name of Oracle nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import javax.activation.*;
/**
* This is a servlet that demonstrates the use of JavaMail APIs
* in a 3-tier application. It allows the user to login to an
* IMAP store, list all the messages in the INBOX folder, view
* selected messages, compose and send a message, and logout.
* <p>
* Please note: This is NOT an example of how to write servlets!
* This is simply to show that JavaMail can be used in a servlet.
* <p>
* For more information on this servlet, see the
* JavaMailServlet.README.txt file.
* <p>
* For more information on servlets, see
*
* @author Max Spivak
*/
/**
* This method handles the "POST" submission from two forms: the
* login form and the message compose form. The login form has the
* following parameters: <code>hostname</code>, <code>username</code>,
* and <code>password</code>. The <code>send</code> parameter denotes
* that the method is processing the compose form submission.
*/
throws ServletException, IOException {
// get the session
// process message sending
} else {
// initial login
// create
try {
throw new MessagingException("No default folder");
throw new MessagingException("Invalid folder");
// track who logged in
// save stuff into MUD
// splash
// folder table
// folder name column header
// msg count column header
// folder name
"Inbox" + "</a></td><br>");
// msg count
totalMessages + "</td>");
} finally {
}
}
}
/**
* This method handles the GET requests for the client.
*/
throws ServletException, IOException {
return;
}
return;
}
// mux that takes a GET request, based on parameters figures
// out what it should do, and routes it to the
// appropriate method
// get url parameters
int msgNum = -1;
int partNum = -1;
// process url params
// operate on message "msgStr"
// display message "msgStr"
} else {
// display part "part" in message "msgStr"
}
// display compose form
// process logout
try {
ses.invalidate();
} catch (MessagingException mex) {
}
} else {
// display headers
}
}
/* main method to display messages */
throws IOException {
"/INBOX</b></font></center><p>");
try {
// first, display this message's headers
// and now, handle the content
//if (o instanceof String) {
//} else if (o instanceof Multipart){
for (int i = 0; i < cnt; i++) {
}
} else {
}
} catch (MessagingException mex) {
}
}
/**
* content parts are displayed inline. For all other parts,
* a URL is generated and displayed; clicking on the URL
* brings up the part in a separate page.
*/
throws IOException {
if (partNum != 0)
try {
return;
}
if (partNum != 0)
} else {
// generate a url for this part
String s;
s = null;
"?message=" +
msgNum + "&part=" +
partNum + "\">Display Attachment</a>");
}
} catch (MessagingException mex) {
}
}
/**
* This method gets the stream from for a given msg part and
* pushes it out to the browser with the correct content type.
* Used to display attachments and relies on the browser's
* content handling capabilities.
*/
throws IOException {
try {
return;
}
int i;
} catch (MessagingException mex) {
}
}
/**
* This is a utility message that pretty-prints the message
* headers for message that is being displayed.
*/
throws IOException {
try {
boolean tf = true;
tf = false;
}
}
boolean tf = true;
tf = false;
}
}
boolean cf = true;
cf = false;
}
}
"<br>");
} catch (MessagingException mex) {
}
}
/**
* This method displays the URL's for the available commands and the
* INBOX headerlist
*/
throws IOException {
"/INBOX</b></font></center><p>");
// URL's for the commands that are available
"?logout=true\">Logout</a>");
"?compose=true\" target=\"compose\">Compose</a>");
// List headers in a table
// sender column header
// date column header
// subject column header
try {
int msgCount = f.getMessageCount();
// for each message, show its headers
for (int i = 1; i <= msgCount; i++) {
m = f.getMessage(i);
// if message has the DELETED flag set, don't display it
continue;
// from
"" ) +
"</font></td>");
// date
m.getSentDate() : m.getReceivedDate()) +
"</font></td>");
// subject & link
"<a href=\"" +
"?message=" +
i + "\">" +
((m.getSubject() != null) ?
m.getSubject() :
"<i>No Subject</i>") +
"</a>" +
"</font></td>");
}
} catch (MessagingException mex) {
}
}
/**
* This method handles the request when the user hits the
* <i>Compose</i> link. It send the compose form to the browser.
*/
throws IOException {
}
/**
* This method processes the send request from the compose form
*/
throws IOException {
try {
throw new Exception("trying to send, but not logged in");
} else
throw new MessagingException("No \"To\" address specified");
}
u.getHost()));
}
}
// utility method; returns a string suitable for msg header display
if (a instanceof InternetAddress &&
} else
return addr;
}
// utility method; retrieve the MailUserData
// from the HttpSession and return it
return null;
} else {
return null;
}
}
return mud;
}
return "A mail reader servlet";
}
/**
* This is the HTML code for the compose form. Another option would
* have been to use a separate html page.
*/
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>";
}
/**
* This class is used to store session data for each user's session. It
* is stored in the HttpSession.
*/
class MailUserData {
}
return url;
}
return session;
}
session = s;
}
return store;
}
store = s;
}
return folder;
}
folder = f;
}
}