DisplayFolders.java
资源名称:某公司的java培训教材 [点击查看]
上传用户:dinglihq
上传日期:2013-02-04
资源大小:99958k
文件大小:3k
源码类别:
Java编程
开发平台:
Java
- package bible.javamail;
- import java.io.*;
- import javax.servlet.*;
- import javax.servlet.http.*;
- import javax.activation.*;
- import javax.mail.*;
- import javax.mail.internet.*;
- import javax.naming.*;
- /**
- * Class DisplayFolders
- *
- *
- * @author
- * @version %I%, %G%
- */
- public class DisplayFolders extends HttpServlet {
- private PrintWriter pw;
- private Session session;
- private Store store;
- private Folder defaultFolder;
- /**
- * Method doGet
- *
- *
- * @param req
- * @param res
- *
- * @throws IOException
- * @throws ServletException
- *
- */
- public void doGet(HttpServletRequest req, HttpServletResponse res)
- throws ServletException, IOException {
- pw = res.getWriter();
- res.setContentType("text/html");
- pw.println("<HTML><HEAD><TITLE>Folders</TITLE></HEAD>");
- pw.println("<BODY><H1>Folders</H1>");
- try {
- getDefaultFolder();
- displayFolders();
- store.close();
- } catch (Exception e) {
- pw.println("<BR><BR><STRONG>" + e.getMessage() + "</STRONG><BR>");
- e.printStackTrace(pw);
- }
- pw.println("</BODY></HTML>");
- }
- private void getDefaultFolder()
- throws NamingException, NoSuchProviderException, MessagingException {
- // lookup mail session in JNDI
- InitialContext context = new InitialContext();
- Session session = (Session) context.lookup("BibleMailSession");
- // get store and create an authenticated session
- store = session.getStore();
- store.connect("xxx.xxx.xxx.xxx", "username", "password");
- // get the default folder
- defaultFolder = store.getDefaultFolder();
- }
- private void displayFolders() throws MessagingException {
- Folder[] allFolders = defaultFolder.list();
- if (allFolders.length > 0) {
- pw.println("<TABLE width='100%'>");
- pw.println("<TR><TD><STRONG>Folder</STRONG></TD>");
- pw.println("<TD><STRONG>Messages</STRONG></TD>");
- pw.println("<TD><STRONG>New</STRONG></TD>");
- pw.println("<TD><STRONG>Unread</STRONG></TD></TR>");
- for (int i = 0; i < allFolders.length; i++) {
- String name = allFolders [i].getName();
- if ((allFolders [i].HOLDS_MESSAGES == Folder.HOLDS_MESSAGES)
- &&!name.equals("Public Folders")) {
- allFolders [i].open(Folder.READ_ONLY);
- pw.println("<TR><TD><A HREF='messages?folder=" + name + "'>" + name
- + "</A></TD></TD>");
- pw.println("<TD>" + allFolders [i].getMessageCount() + "</TD>");
- pw.println("<TD>" + allFolders [i].getNewMessageCount() + "</TD>");
- pw.println("<TD>" + allFolders [i].getUnreadMessageCount()
- + "</TD></TR>");
- allFolders [i].close(false);
- }
- }
- pw.println("</TABLE>");
- }
- }
- }
- /*--- Formatted in Bible Style on Thu, Sep 6, '01 ---*/
- /*------ Formatted by Jindent 3.24 Gold 1.02 --- http://www.jindent.de ------*/