DisplayMessage.java
资源名称:某公司的java培训教材 [点击查看]
上传用户:dinglihq
上传日期:2013-02-04
资源大小:99958k
文件大小:9k
源码类别:
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 DisplayMessage
- *
- *
- * @author
- * @version %I%, %G%
- */
- public class DisplayMessage extends HttpServlet {
- private PrintWriter pw;
- private Session session;
- private Store store;
- private Folder defaultFolder;
- private Folder folder;
- private String folderName;
- private int msgid;
- 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");
- String cmd = req.getParameter("cmd");
- folderName = req.getParameter("folder");
- msgid = new Integer(req.getParameter("msgid")).intValue();
- // display message
- if (cmd.equals("display")) {
- displayMessage();
- // delete message
- } else if (cmd.equals("delete")) {
- deleteMessage();
- RequestDispatcher rd =
- getServletContext().getNamedDispatcher("DisplayMessages");
- rd.forward(req, res);
- }
- }
- private void getDefaultFolder()
- throws NamingException, NoSuchProviderException, MessagingException {
- // lookup mail session in JNDI
- InitialContext context = new InitialContext();
- 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 getMessage() throws MessagingException, NamingException {
- // try to open foler read/write otherwise read-only
- getDefaultFolder();
- folder = defaultFolder.getFolder(folderName);
- try {
- folder.open(Folder.READ_WRITE);
- } catch (MessagingException ex) {
- folder.open(Folder.READ_ONLY);
- }
- // get message
- message = folder.getMessage(msgid);
- }
- private void displayMessage() {
- pw.println("<HTML><HEAD><TITLE>Message Detail</TITLE></HEAD>");
- pw.println("<BODY><H1>Message Detail</H1>");
- try {
- getMessage();
- // display the message
- pw.println("<TABLE width='100%'>");
- pw.println("<TR><TD>Sent:</TD><TD>" + getSent() + "</TD></TR>");
- pw.println("<TR><TD>Received:</TD><TD>" + getReceived() + "</TD></TR>");
- pw.println("<TR><TD>From:</TD><TD>" + getFrom() + "</TD></TR>");
- pw.println("<TR><TD>Reply to:</TD><TD>" + getReplyTo() + "</TD></TR>");
- pw.println("<TR><TD>To:</TD><TD>" + getTo() + "</TD></TR>");
- pw.println("<TR><TD>Cc:</TD><TD>" + getCc() + "</TD></TR>");
- pw.println("<TR><TD>Subject:</TD><TD>" + getSubject() + "</TD></TR>");
- pw.println("<TR><TD>Size:</TD><TD>" + getSize() + "</TD></TR>");
- pw.println("<TR><TD>Flags:</TD><TD>" + getFlags() + "</TD></TR>");
- pw.println("<TR><TD VALIGN>Message:</TD><TD>" + getMessageText()
- + "</TD></TR>");
- String enclosure = getEnclosureName();
- pw.print("<TR><TD>Enclosure:</TD><TD>" + enclosure + "</TD></TR>");
- pw.print("<TR><TD><BR><A HREF='message?cmd=delete&folder=" + folderName
- + "&msgid=" + msgid + "'>Delete</A></TD></TR>");
- pw.println("</TABLE>");
- folder.close(false);
- store.close();
- } catch (Exception e) {
- pw.println("<BR><BR><STRONG>" + e.getMessage() + "</STRONG><BR>");
- e.printStackTrace(pw);
- }
- pw.println("</BODY></HTML>");
- }
- private void deleteMessage() {
- try {
- getMessage();
- message.setFlag(Flags.Flag.DELETED, true);
- } catch (Exception e) {}
- }
- /**
- * Method getSent
- *
- *
- * @return
- *
- * @throws MessagingException
- *
- */
- public String getSent() throws MessagingException {
- Date sentDate = message.getSentDate();
- return DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
- DateFormat.SHORT).format(sentDate);
- }
- /**
- * 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;
- }
- /**
- * Method getFrom
- *
- *
- * @return
- *
- * @throws MessagingException
- *
- */
- public String getFrom() throws MessagingException {
- Address[] addresses = message.getFrom();
- return addresses [0].toString();
- }
- /**
- * Method getReplyTo
- *
- *
- * @return
- *
- * @throws MessagingException
- *
- */
- public String getReplyTo() throws MessagingException {
- StringBuffer sb = new StringBuffer();
- Address[] addresses = message.getReplyTo();
- if (addresses != null) {
- for (int j = 0; j < addresses.length; j++) {
- sb.append(addresses [j].toString() + " ");
- }
- }
- return sb.toString();
- }
- /**
- * Method getTo
- *
- *
- * @return
- *
- * @throws MessagingException
- *
- */
- public String getTo() throws MessagingException {
- StringBuffer sb = new StringBuffer();
- Address[] addresses = message.getRecipients(Message.RecipientType.TO);
- if (addresses != null) {
- for (int j = 0; j < addresses.length; j++) {
- sb.append(addresses [j].toString() + " ");
- }
- }
- return sb.toString();
- }
- /**
- * Method getCc
- *
- *
- * @return
- *
- * @throws MessagingException
- *
- */
- public String getCc() throws MessagingException {
- StringBuffer sb = new StringBuffer();
- Address[] addresses = message.getRecipients(Message.RecipientType.CC);
- if (addresses != null) {
- for (int j = 0; j < addresses.length; j++) {
- sb.append(addresses [j].toString() + " ");
- }
- }
- return sb.toString();
- }
- /**
- * Method getSubject
- *
- *
- * @return
- *
- * @throws MessagingException
- *
- */
- public String getSubject() throws MessagingException {
- return message.getSubject();
- }
- 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 String getFlags() throws MessagingException {
- Flags flags = message.getFlags();
- StringBuffer sb = new StringBuffer();
- Flags.Flag[] sysFlags = flags.getSystemFlags();
- for (int i = 0; i < sysFlags.length; i++) {
- if (sysFlags [i] == Flags.Flag.ANSWERED) {
- sb.append("Answered ");
- } else if (sysFlags [i] == Flags.Flag.DELETED) {
- sb.append("Deleted ");
- } else if (sysFlags [i] == Flags.Flag.DRAFT) {
- sb.append("Draft ");
- } else if (sysFlags [i] == Flags.Flag.FLAGGED) {
- sb.append("Flagged ");
- } else if (sysFlags [i] == Flags.Flag.RECENT) {
- sb.append("Recent ");
- } else if (sysFlags [i] == Flags.Flag.SEEN) {
- sb.append("Seen ");
- } else if (sysFlags [i] == Flags.Flag.USER) {
- sb.append("User ");
- } else {
- sb.append("Unknown ");
- }
- }
- String[] userFlags = flags.getUserFlags();
- for (int i = 0; i < userFlags.length; i++) {
- sb.append(userFlags [i].toString() + " ");
- }
- return sb.toString();
- }
- /**
- * Method getMessageText
- *
- *
- * @return
- *
- * @throws IOException
- * @throws MessagingException
- *
- */
- public String getMessageText() throws IOException, MessagingException {
- String text = "";
- if (message.isMimeType("text/plain")) {
- text = (String) message.getContent();
- } else if (message.isMimeType("multipart/*")) {
- Multipart mp = (Multipart) message.getContent();
- BodyPart bp = mp.getBodyPart(0);
- if (bp.isMimeType("text/plain") | bp.isMimeType("text/html")) {
- text = (String) bp.getContent();
- }
- }
- return text;
- }
- private String getEnclosureName() throws MessagingException, IOException {
- StringBuffer sb = new StringBuffer();
- if (message.isMimeType("multipart/*")) {
- Multipart mp = (Multipart) message.getContent();
- int count = mp.getCount();
- for (int i = 0; i < count; i++) {
- BodyPart bp = mp.getBodyPart(i);
- if (bp.getFileName() != null) {
- sb.append(bp.getFileName() + " ");
- }
- }
- }
- return sb.toString();
- }
- }
- /*--- Formatted in Bible Style on Thu, Sep 6, '01 ---*/
- /*------ Formatted by Jindent 3.24 Gold 1.02 --- http://www.jindent.de ------*/