DisplayMessages.java
资源名称:某公司的java培训教材 [点击查看]
上传用户:dinglihq
上传日期:2013-02-04
资源大小:99958k
文件大小:6k
源码类别:
Java编程
开发平台:
Java
- package bible.javamail;
- import java.io.*;
- import javax.servlet.*;
- import javax.servlet.http.*;
- import java.util.*;
- import java.text.*;
- import javax.activation.*;
- import javax.mail.*;
- import javax.mail.internet.*;
- import javax.naming.*;
- /**
- * Class DisplayMessages
- *
- *
- * @author
- * @version %I%, %G%
- */
- public class DisplayMessages extends HttpServlet {
- private PrintWriter pw;
- private Session session;
- private Store store;
- private Folder defaultFolder;
- private Folder folder;
- private String folderName;
- private Message message;
- /**
- * 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");
- folderName = req.getParameter("folder");
- pw.println("<HTML><HEAD><TITLE>Messages</TITLE></HEAD>");
- pw.println("<BODY><H1>Messages</H1>");
- try {
- getDefaultFolder();
- displayMessages(folderName);
- } 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 displayMessages(String folderName)
- throws MessagingException, IOException {
- pw.println("<A HREF='sendmail2'>Compose Message</A><BR>");
- pw.println("<H3>Folder: " + folderName + "</H3>");
- folder = defaultFolder.getFolder(folderName);
- folder.open(Folder.READ_ONLY);
- Message[] messages = folder.getMessages();
- if (messages.length > 0) {
- pw.println("<TABLE width='100%'>");
- pw.println("<TR><TD><STRONG>From</STRONG></TD>");
- pw.println("<TD><STRONG>Subject</STRONG></TD>");
- pw.println("<TD><STRONG>Received</STRONG></TD>");
- pw.println("<TD><STRONG>Size</STRONG></TD>");
- pw.println("<TD><STRONG>Enclosure</STRONG></TD>");
- pw.println("<TD><STRONG>Seen</STRONG></TD></TR>");
- // loop through messages
- for (int i = 0; i < messages.length; i++) {
- // skip deleted messages
- if (!messages [i].isSet(Flags.Flag.DELETED)) {
- message = messages [i];
- pw.println("<TR><TD>" + getFrom() + "</TD>");
- String subject = ("".equals(getSubject())
- ? "None"
- : getSubject());
- pw.println("<TD><A HREF='message?cmd=display&folder=" + folderName
- + "&msgid=" + getMessageNumber() + "'>" + subject
- + "</A></TD>");
- pw.println("<TD>" + getReceived() + "</TD>");
- pw.println("<TD>" + getSize() + "</TD>");
- pw.println("<TD>" + hasEnclosure() + "</TD>");
- pw.println("<TD>" + hasBeenSeen() + "</TD></TR>");
- }
- }
- pw.println("</TABLE>");
- folder.close(false);
- store.close();
- }
- }
- /**
- * Method getFrom
- *
- *
- * @return
- *
- * @throws MessagingException
- *
- */
- public String getFrom() throws MessagingException {
- Address[] addresses = message.getFrom();
- return addresses [0].toString();
- }
- /**
- * Method getSubject
- *
- *
- * @return
- *
- * @throws MessagingException
- *
- */
- public String getSubject() throws MessagingException {
- return message.getSubject();
- }
- /**
- * Method getMessageNumber
- *
- *
- * @return
- *
- * @throws MessagingException
- *
- */
- public int getMessageNumber() throws MessagingException {
- return message.getMessageNumber();
- }
- /**
- * Method getReceived
- *
- *
- * @return
- *
- * @throws MessagingException
- *
- */
- public String getReceived() throws MessagingException {
- String date = "";
- Date receivedDate = message.getReceivedDate();
- if (receivedDate != null) {
- date =
- DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
- DateFormat.SHORT).format(receivedDate);
- }
- return date;
- }
- private String getSize() throws MessagingException {
- int size = message.getSize();
- String sizeDesc = size + " B";
- if (size >= 1024) {
- size /= 1024;
- sizeDesc = size + " KB";
- if (size >= 1024) {
- size /= 1024;
- sizeDesc = size + " MB";
- }
- }
- return sizeDesc;
- }
- private boolean hasEnclosure() throws MessagingException, IOException {
- boolean b = false;
- if (message.isMimeType("multipart/*")) {
- Multipart mp = (Multipart) message.getContent();
- int count = mp.getCount();
- for (int j = 0; j < count; j++) {
- BodyPart bp = mp.getBodyPart(j);
- if (bp.getFileName() != null) {
- b = true;
- }
- }
- }
- return b;
- }
- private boolean hasBeenSeen() throws MessagingException {
- boolean b = false;
- if (message.isSet(Flags.Flag.SEEN)) {
- b = true;
- }
- return b;
- }
- }
- /*--- Formatted in Bible Style on Thu, Sep 6, '01 ---*/
- /*------ Formatted by Jindent 3.24 Gold 1.02 --- http://www.jindent.de ------*/