List of usage examples for javax.mail Folder getMode
public synchronized int getMode()
From source file:net.wastl.webmail.server.WebMailSession.java
/** * Fetch a message from a folder./*from www. ja v a 2 s . com*/ * Will put the messages parameters in the sessions environment * * @param foldername Name of the folder were the message should be fetched from * @param msgnum Number of the message to fetch * @param mode there are three different modes: standard, reply and forward. reply and forward will enter the message * into the current work element of the user and set some additional flags on the message if the user * has enabled this option. * @see net.wastl.webmail.server.WebMailSession.GETMESSAGE_MODE_STANDARD * @see net.wastl.webmail.server.WebMailSession.GETMESSAGE_MODE_REPLY * @see net.wastl.webmail.server.WebMailSession.GETMESSAGE_MODE_FORWARD */ public void getMessage(String folderhash, int msgnum, int mode) throws NoSuchFolderException, WebMailException { // security reasons: // attachments=null; try { TimeZone tz = TimeZone.getDefault(); DateFormat df = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.SHORT, user.getPreferredLocale()); df.setTimeZone(tz); Folder folder = getFolder(folderhash); Element xml_folder = model.getFolder(folderhash); if (folder == null) { throw new NoSuchFolderException("No such folder: " + folderhash); } if (folder.isOpen() && folder.getMode() == Folder.READ_WRITE) { folder.close(false); folder.open(Folder.READ_ONLY); } else if (!folder.isOpen()) { folder.open(Folder.READ_ONLY); } MimeMessage m = (MimeMessage) folder.getMessage(msgnum); String messageid; try { StringTokenizer tok = new StringTokenizer(m.getMessageID(), "<>"); messageid = tok.nextToken(); } catch (NullPointerException ex) { // For mail servers that don't generate a Message-ID (Outlook et al) messageid = user.getLogin() + "." + msgnum + ".jwebmail@" + user.getDomain(); } Element xml_current = model.setCurrentMessage(messageid); XMLMessage xml_message = model.getMessage(xml_folder, m.getMessageNumber() + "", messageid); /* Check whether we already cached this message (not only headers but complete)*/ boolean cached = xml_message.messageCompletelyCached(); /* If we cached the message, we don't need to fetch it again */ if (!cached) { //Element xml_header=model.getHeader(xml_message); try { String from = MimeUtility.decodeText(Helper.joinAddress(m.getFrom())); String replyto = MimeUtility.decodeText(Helper.joinAddress(m.getReplyTo())); String to = MimeUtility .decodeText(Helper.joinAddress(m.getRecipients(Message.RecipientType.TO))); String cc = MimeUtility .decodeText(Helper.joinAddress(m.getRecipients(Message.RecipientType.CC))); String bcc = MimeUtility .decodeText(Helper.joinAddress(m.getRecipients(Message.RecipientType.BCC))); Date date_orig = m.getSentDate(); String date = getStringResource("no date"); if (date_orig != null) { date = df.format(date_orig); } String subject = ""; if (m.getSubject() != null) { subject = MimeUtility.decodeText(m.getSubject()); } if (subject == null || subject.equals("")) { subject = getStringResource("no subject"); } try { Flags.Flag[] sf = m.getFlags().getSystemFlags(); for (int j = 0; j < sf.length; j++) { if (sf[j] == Flags.Flag.RECENT) xml_message.setAttribute("recent", "true"); if (sf[j] == Flags.Flag.SEEN) xml_message.setAttribute("seen", "true"); if (sf[j] == Flags.Flag.DELETED) xml_message.setAttribute("deleted", "true"); if (sf[j] == Flags.Flag.ANSWERED) xml_message.setAttribute("answered", "true"); if (sf[j] == Flags.Flag.DRAFT) xml_message.setAttribute("draft", "true"); if (sf[j] == Flags.Flag.FLAGGED) xml_message.setAttribute("flagged", "true"); if (sf[j] == Flags.Flag.USER) xml_message.setAttribute("user", "true"); } } catch (NullPointerException ex) { } if (m.getContentType().toUpperCase().startsWith("MULTIPART/")) { xml_message.setAttribute("attachment", "true"); } int size = m.getSize(); size /= 1024; xml_message.setAttribute("size", (size > 0 ? size + "" : "<1") + " kB"); /* Set all of what we found into the DOM */ xml_message.setHeader("FROM", from); xml_message.setHeader("SUBJECT", Fancyfier.apply(subject)); xml_message.setHeader("TO", to); xml_message.setHeader("CC", cc); xml_message.setHeader("BCC", bcc); xml_message.setHeader("REPLY-TO", replyto); xml_message.setHeader("DATE", date); /* Decode MIME contents recursively */ xml_message.removeAllParts(); parseMIMEContent(m, xml_message, messageid); } catch (UnsupportedEncodingException e) { log.warn("Unsupported Encoding in parseMIMEContent: " + e.getMessage()); } } /* Set seen flag (Maybe make that threaded to improve performance) */ if (user.wantsSetFlags()) { if (folder.isOpen() && folder.getMode() == Folder.READ_ONLY) { folder.close(false); folder.open(Folder.READ_WRITE); } else if (!folder.isOpen()) { folder.open(Folder.READ_WRITE); } folder.setFlags(msgnum, msgnum, new Flags(Flags.Flag.SEEN), true); folder.setFlags(msgnum, msgnum, new Flags(Flags.Flag.RECENT), false); if ((mode & GETMESSAGE_MODE_REPLY) == GETMESSAGE_MODE_REPLY) { folder.setFlags(msgnum, msgnum, new Flags(Flags.Flag.ANSWERED), true); } } folder.close(false); /* In this part we determine whether the message was requested so that it may be used for further editing (replying or forwarding). In this case we set the current "work" message to the message we just fetched and then modifiy it a little (quote, add a "Re" to the subject, etc). */ XMLMessage work = null; if ((mode & GETMESSAGE_MODE_REPLY) == GETMESSAGE_MODE_REPLY || (mode & GETMESSAGE_MODE_FORWARD) == GETMESSAGE_MODE_FORWARD) { log.debug("Setting work message!"); work = model.setWorkMessage(xml_message); String newmsgid = WebMailServer.generateMessageID(user.getUserName()); if (work != null && (mode & GETMESSAGE_MODE_REPLY) == GETMESSAGE_MODE_REPLY) { String from = work.getHeader("FROM"); work.setHeader("FROM", user.getDefaultEmail()); work.setHeader("TO", from); work.prepareReply(getStringResource("reply subject prefix"), getStringResource("reply subject postfix"), getStringResource("reply message prefix"), getStringResource("reply message postfix")); } else if (work != null && (mode & GETMESSAGE_MODE_FORWARD) == GETMESSAGE_MODE_FORWARD) { String from = work.getHeader("FROM"); work.setHeader("FROM", user.getDefaultEmail()); work.setHeader("TO", ""); work.setHeader("CC", ""); work.prepareForward(getStringResource("forward subject prefix"), getStringResource("forward subject postfix"), getStringResource("forward message prefix"), getStringResource("forward message postfix")); /* Copy all references to MIME parts to the new message id */ for (String key : getMimeParts(work.getAttribute("msgid"))) { StringTokenizer tok2 = new StringTokenizer(key, "/"); tok2.nextToken(); String newkey = tok2.nextToken(); mime_parts_decoded.put(newmsgid + "/" + newkey, mime_parts_decoded.get(key)); } } /* Clear the msgnr and msgid fields at last */ work.setAttribute("msgnr", "0"); work.setAttribute("msgid", newmsgid); prepareCompose(); } } catch (MessagingException ex) { log.error("Failed to get message. Doing nothing instead.", ex); } }
From source file:net.wastl.webmail.server.WebMailSession.java
/** Change the Flags of the messages the user selected. //ww w. j a v a 2 s. c om */ public void setFlags(String folderhash, HTTPRequestHeader head) throws MessagingException { if (head.isContentSet("copymovemsgs") && head.getContent("COPYMOVE").equals("COPY")) { copyMoveMessage(folderhash, head.getContent("TO"), head, false); } else if (head.isContentSet("copymovemsgs") && head.getContent("COPYMOVE").equals("MOVE")) { copyMoveMessage(folderhash, head.getContent("TO"), head, true); } else if (head.isContentSet("flagmsgs")) { log.debug("setting message flags"); Folder folder = getFolder(folderhash); //log.debug("Processing Request Header..."); /* Get selected messages */ int msgs[] = getSelectedMessages(head, folder.getMessageCount()); //log.debug("get flags..."); /* Get selected flags */ Flags fl = new Flags(Flags.Flag.USER); if (head.getContent("MESSAGE FLAG").equals("DELETED")) { fl = new Flags(Flags.Flag.DELETED); if (need_expunge_folders == null) { need_expunge_folders = new Vector<String>(); } need_expunge_folders.addElement(folderhash); } else if (head.getContent("MESSAGE FLAG").equals("SEEN")) { fl = new Flags(Flags.Flag.SEEN); } else if (head.getContent("MESSAGE FLAG").equals("RECENT")) { fl = new Flags(Flags.Flag.RECENT); } else if (head.getContent("MESSAGE FLAG").equals("ANSWERED")) { fl = new Flags(Flags.Flag.ANSWERED); } else if (head.getContent("MESSAGE FLAG").equals("DRAFT")) { fl = new Flags(Flags.Flag.DRAFT); } boolean value = true; if (head.getContent("MARK").equals("UNMARK")) { value = false; } //log.debug("Done!"); //log.debug("Setting flags..."); if (user.wantsSetFlags()) { if (folder.isOpen() && folder.getMode() == Folder.READ_ONLY) { folder.close(false); folder.open(Folder.READ_WRITE); } else if (!folder.isOpen()) { folder.open(Folder.READ_WRITE); } folder.setFlags(msgs, fl, value); if (user.getBoolVar("autoexpunge")) { folder.close(true); if (need_expunge_folders != null) { need_expunge_folders.removeElement(folderhash); } } else { folder.close(false); } } folder.open(Folder.READ_WRITE); refreshFolderInformation(folderhash); } }
From source file:net.wastl.webmail.server.WebMailSession.java
/** * Copy or move the selected messages from folder fromfolder to folder tofolder. *//*w w w. ja v a 2s . co m*/ public void copyMoveMessage(String fromfolder, String tofolder, HTTPRequestHeader head, boolean move) throws MessagingException { Folder from = getFolder(fromfolder); Folder to = getFolder(tofolder); if (user.wantsSetFlags()) { if (from.isOpen() && from.getMode() == Folder.READ_ONLY) { from.close(false); from.open(Folder.READ_WRITE); } else if (!from.isOpen()) { from.open(Folder.READ_WRITE); } if (to.isOpen() && to.getMode() == Folder.READ_ONLY) { to.close(false); to.open(Folder.READ_WRITE); } else if (!to.isOpen()) { to.open(Folder.READ_WRITE); } } else { if (!from.isOpen()) { from.open(Folder.READ_ONLY); } if (to.isOpen() && to.getMode() == Folder.READ_ONLY) { to.close(false); to.open(Folder.READ_WRITE); } else if (!to.isOpen()) { to.open(Folder.READ_WRITE); } } int m[] = getSelectedMessages(head, from.getMessageCount()); Message msgs[] = from.getMessages(m); from.copyMessages(msgs, to); if (move && user.wantsSetFlags()) { from.setFlags(m, new Flags(Flags.Flag.DELETED), true); if (user.getBoolVar("autoexpunge")) { from.close(true); to.close(true); } else { if (need_expunge_folders == null) { need_expunge_folders = new Vector<String>(); } need_expunge_folders.addElement(fromfolder); from.close(false); to.close(false); } } else { from.close(false); if (user.getBoolVar("autoexpunge")) { to.close(true); } else { to.close(false); } } from.open(Folder.READ_WRITE); to.open(Folder.READ_WRITE); refreshFolderInformation(fromfolder); refreshFolderInformation(tofolder); }