www.pudn.com > javamail-1_3_3_01.zip > JavaMailServlet.java, change:2005-09-09,size:22249b
/* * @(#)JavaMailServlet.java 1.5 01/05/23 * * Copyright 1998, 1999 Sun Microsystems, Inc. All Rights Reserved. * * 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. * * - Redistribution in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of Sun Microsystems, Inc. or the names of contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * This software is provided "AS IS," without a warranty of any kind. ALL * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND * ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES OR LIABILITIES * SUFFERED BY LICENSEE AS A RESULT OF OR RELATING TO USE, MODIFICATION * OR DISTRIBUTION OF THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL * SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, * ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. * * You acknowledge that Software is not designed, licensed or intended * for use in the design, construction, operation or maintenance of any * nuclear facility. */ import java.io.*; import java.util.*; import java.text.*; import javax.servlet.*; import javax.servlet.http.*; import javax.mail.*; import javax.mail.internet.*; 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. ** 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. *
* For more information on this servlet, see the * JavaMailServlet.README.txt file. *
* For more information on servlets, see * * http://java.sun.com/products/java-server/servlets/index.html * * @author Max Spivak */ public class JavaMailServlet extends HttpServlet implements SingleThreadModel { String protocol = "imap"; String mbox = "INBOX"; /** * This method handles the "POST" submission from two forms: the * login form and the message compose form. The login form has the * following parameters:
"); if (send != null) { // process message sending send(req, res, out, ssn); } else { // initial login // create MailUserData mud = new MailUserData(url); ssn.putValue("javamailservlet", mud); try { Properties props = System.getProperties(); props.put("mail.smtp.host", host); Session session = Session.getDefaultInstance(props, null); session.setDebug(false); Store store = session.getStore(url); store.connect(); Folder folder = store.getDefaultFolder(); if (folder == null) throw new MessagingException("No default folder"); folder = folder.getFolder(mbox); if (folder == null) throw new MessagingException("Invalid folder"); folder.open(Folder.READ_WRITE); int totalMessages = folder.getMessageCount(); Message[] msgs = folder.getMessages(); FetchProfile fp = new FetchProfile(); fp.add(FetchProfile.Item.ENVELOPE); folder.fetch(msgs, fp); // track who logged in System.out.println("Login from: " + store.getURLName()); // save stuff into MUD mud.setSession(session); mud.setStore(store); mud.setFolder(folder); // splash out.print("hostname,username, * andpassword. Thesendparameter denotes * that the method is processing the compose form submission. */ public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { // get the session HttpSession ssn = req.getSession(true); String send = req.getParameter("send"); String host = req.getParameter("hostname"); String user = req.getParameter("username"); String passwd = req.getParameter("password"); URLName url = new URLName(protocol, host, -1, mbox, user, passwd); ServletOutputStream out = res.getOutputStream(); res.setContentType("text/html"); out.println(""); out.print(""); out.println("Welcome to JavaMail! "); // folder table out.println("
| "); out.print(""); out.println("FolderName | "); out.print(""); out.println("Messages |
| "); out.print("" + "Inbox" + " | " + totalMessages + " | "); out.println("
"); try { Message msg = mud.getFolder().getMessage(msgNum); // first, display this message's headers displayMessageHeaders(mud, msg, out); // and now, handle the content Object o = msg.getContent(); //if (o instanceof String) { if (msg.isMimeType("text/plain")) { out.println("
");
out.println((String)o);
out.println("");
//} else if (o instanceof Multipart){
} else if (msg.isMimeType("multipart/*")) {
Multipart mp = (Multipart)o;
int cnt = mp.getCount();
for (int i = 0; i < cnt; i++) {
displayPart(mud, msgNum, mp.getBodyPart(i), i, req, out);
}
} else {
out.println(msg.getContentType());
}
} catch (MessagingException mex) {
out.println(mex.toString());
}
out.println("");
out.close();
}
/**
* This method displays a message part. text/plain
* 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.
*/
private void displayPart(MailUserData mud, int msgNum, Part part,
int partNum, HttpServletRequest req,
ServletOutputStream out)
throws IOException {
if (partNum != 0)
out.println("");
out.println((String)part.getContent());
out.println("");
} else {
// generate a url for this part
String s;
if ((s = part.getFileName()) != null)
out.println("Filename: " + s + ""); // URL's for the commands that are available out.println(""); out.println("Logout"); out.println("Compose"); out.println(""); out.println("
| "); out.println(""); out.println("Sender | "); // date column header out.println(""); out.println(""); out.println("Date | "); // subject column header out.println(""); out.println(""); out.println("Subject |
| "); out.println("" + ((m.getFrom() != null) ? m.getFrom()[0].toString() : "" ) + " | "); // date out.print(""); out.println("" + df.format((m.getSentDate()!=null) ? m.getSentDate() : m.getReceivedDate()) + " | "); // subject & link out.print(""); out.println("" + "" + ((m.getSubject() != null) ? m.getSubject() : "No Subject") + "" + " | "); out.println("
| " + mex.toString() + " |