Package com.sun.mail.imap

Examples of com.sun.mail.imap.IMAPFolder$ProtocolCommand


   */
  private void checkIMAPFolder(int folderID, MailAccountVO accountVO)
  {
    CVDal cvdal = new CVDal(this.dataSource);
    IMAPStore store = null;
    IMAPFolder folder = null;
    try {
      Session session = null;
      store = this.setupIMAPConnection(accountVO, session);
      MailFolderVO folderVO = this.getEmailFolder(folderID, cvdal);

      String name = folderVO.getFolderFullName();
      if (name == null) {
        name = folderVO.getFolderName();
      }
      folder = (IMAPFolder)store.getFolder(name);

      int type = folder.getType();
      if ((type & IMAPFolder.HOLDS_MESSAGES) != 0) {
        folder.open(Folder.READ_WRITE);
        Message msgs[] = folder.getMessages();
        FetchProfile fp = new FetchProfile();
        fp.add(IMAPFolder.FetchProfileItem.FLAGS);
        fp.add(UIDFolder.FetchProfileItem.UID);
        folder.fetch(msgs, fp);
        this.handleIMAPMessageArray(msgs, folder, folderID, accountVO, store, cvdal);
      }
    } catch (Exception e) {
      logger.error("[checkIMAPFolder] Exception thrown.", e);
    } finally {
      try {
        if (folder.isOpen()) {
          folder.close(false);
        }
      } catch (Exception e) {}
      try { store.close(); } catch(MessagingException e) {}
      cvdal.destroy();
    }
View Full Code Here


    IMAPStore store = null;
    try { //take this try/catch away
      Session session = null;
      store = this.setupIMAPConnection(accountVO, session);

      IMAPFolder root = (IMAPFolder)store.getDefaultFolder();
      IMAPFolder[] allFolders = (IMAPFolder[])root.list("*");
      HashMap folders = new HashMap();

      for (int i = 0; i < allFolders.length; i++) {
        if (allFolders[i].isSubscribed() || allFolders[i].getName().equals("INBOX")) {
          folders.put(allFolders[i].getFullName(), allFolders[i]);
        }
      }

      this.processIMAPFolders(folders, accountVO, cvdal);

      Iterator iter = folders.values().iterator();
      while (iter.hasNext()) {
        IMAPFolder current = (IMAPFolder)iter.next();
        MailFolderVO folderVO = this.getEmailFolderByName(accountVO.getAccountID(), current.getFullName(), cvdal);
        int folderID = folderVO.getFolderID();
        if (folderID == -1) {
          folderVO = this.getEmailFolderByName(accountVO.getAccountID(), current.getName(), cvdal);
          folderID = folderVO.getFolderID();
        }

        int type = current.getType();
        if ((type & IMAPFolder.HOLDS_MESSAGES) != 0) {
          current.open(Folder.READ_WRITE);
          Message msgs[] = current.getMessages();
          FetchProfile fp = new FetchProfile();
          fp.add(IMAPFolder.FetchProfileItem.FLAGS);
          fp.add(UIDFolder.FetchProfileItem.UID);
          current.fetch(msgs, fp);
          this.handleIMAPMessageArray(msgs, current, folderID, accountVO, store, cvdal);
        }
        if (current.isOpen()) {
          current.close(false);
        }
      }
    } catch (Exception e) {
      logger.error("[newIMAP] Exception thrown.", e);
    } finally {
View Full Code Here

    Set fullPathKeys = folders.keySet();
    try { //remove
      Iterator iter = fullPathKeys.iterator();
      while (iter.hasNext()) {
        String key = (String)iter.next();
        IMAPFolder current = (IMAPFolder)folders.get(key);
        char separator = current.getSeparator();
        key = key.replaceFirst("^" + separator, "");
        String elements[] = key.split(String.valueOf(separator));
        int folderID = 0;
        int parentID = this.getRootFolderId(accountVO.getAccountID(), cvdal);
        for (int i = 0; i < elements.length; i++) {
View Full Code Here

          String name = toFolderVO.getFolderFullName();
          if (name == null) {
            name = toFolderVO.getFolderName();
          }
          IMAPFolder toFolder = (IMAPFolder)store.getFolder(name);

          name = fromFolderVO.getFolderFullName();
          if (name == null) {
            name = fromFolderVO.getFolderName();
          }
          IMAPFolder fromFolder = (IMAPFolder)store.getFolder(name);

          fromFolder.open(IMAPFolder.READ_WRITE);
          Message msg = fromFolder.getMessageByUID(Long.parseLong(messageVO.getMessageUID()));
          fromFolder.copyMessages(new Message[] {msg}, toFolder);
          msg.setFlag(Flags.Flag.DELETED, true); //??
          fromFolder.close(true);
        } catch(Exception e) {
          System.out.println("Exception thrown in moveMessageToFolder()");
          e.printStackTrace();
          return(0);
        }
View Full Code Here

        String name = toFolderVO.getFolderFullName();
        if (name == null) {
          name = toFolderVO.getFolderName();
        }
        IMAPFolder toFolder = (IMAPFolder)store.getFolder(name);

        name = fromFolderVO.getFolderFullName();
        if (name == null) {
          name = fromFolderVO.getFolderName();
        }
        IMAPFolder fromFolder = (IMAPFolder)store.getFolder(name);

        fromFolder.open(IMAPFolder.READ_ONLY);
        Message msg = fromFolder.getMessageByUID(Long.parseLong(messageVO.getMessageUID()));
        fromFolder.copyMessages(new Message[] {msg}, toFolder);
        fromFolder.close(false);
      } catch(Exception e) {
        System.out.println("Exception thrown in copyMessageToFolder()");
        e.printStackTrace();
        return(0);
      }
View Full Code Here

      ArrayList problemFolders = new ArrayList();

      // process each folder in the "folders" array, and *possibly* the List "reprocessFolders"
      for (int i=0,k=0; (i < folders.length) || (k != 0) ; i++) {
        // get the folder for this iteration...
        IMAPFolder currentFolder = null;
        if ((k != 0) && (i >= folders.length)) {
          // in this case, we have finished processing the "folders" array,
          // and we are now processing the "reprocessFolders" List. Get the
          // currentFolder from the IMAP server ("store").
          k--;
          String newFolderPath = (String)reprocessFolders.get(k);
          currentFolder = (IMAPFolder)store.getFolder(newFolderPath);
        }else{
          // in this case, we're looping through the "folders" array for the
          // first time, so get the currentFolder from the "folders" array.
          currentFolder = folders[i];
        }

        String currentFolderName = currentFolder.getName();

        if (! currentFolder.isSubscribed() && (! currentFolderName.equals("INBOX"))) {
          continue;
        }

        // folderFullPath should contain the PATH to the folder, *EXcluding*
        // current folder name. (if the current folder is /Inbox/Personal/Jokes,
        // then folderFullPath should be /Inbox/Personal). This will be used
        // to get the parent ID of the local folder so that we can create the
        // new folder locally.
        String folderFullPath = "/root/";
        String remoteFullName = currentFolder.getFullName();
        folderFullPath += remoteFullName;
        folderFullPath = folderFullPath.replaceAll(currentFolderName+"$", "");
        folderFullPath = folderFullPath.replaceAll("/$", "");

        String folderFullName = remoteFullName.replaceAll("/$", "");

        if (k != 0) {
          // If we are finished processing the "folders" array, and are
          // now currently processing the "reprocessFolders" ArrayList,
          // we must remove the object from reprocessFolders, so that we
          // won't process that folder yet again
          if (reprocessFolders.contains(folderFullName)) {
            reprocessFolders.remove(folderFullName);
            k--;
          }
        }

        try {
          currentFolder.open(Folder.READ_WRITE);
        }catch(MessagingException me){
          System.out.println("[MailEJB][Exception] A messaging exception was caught while opening [" + folderFullName + "].");
          System.out.println("[MailEJB][Exception] skipping this folder, and continuing to process other folders.");
          me.printStackTrace();
          continue;
        }

        int folderID = -1;
        MailFolderVO currentFolderVO = this.getEmailFolderByName(mailAccountVO.getAccountID(), currentFolder.getName(), cvdal);

        // Get the remote folders, check if they exist locally, and CRUD them if necessary
        // TODO: when checking if IMAP folder exists locally, we need to pass full path, not just folder name
        if (this.emailFolderExists(mailAccountVO.getAccountID(), currentFolder.getName(), cvdal)) {
          // folder exists locally, just get the ID
          folderID = currentFolderVO.getFolderID();
        }else{
          // folder does not exist locally, create a new one and get the new ID
          // THIS CODE NEEDS TO BE ABSTRACTED OUT INTO A NEW METHOD, BECAUSE IT
          // IS IDENTICAL TO SOME CODE DOWN BELOW AFTER THE FOLDERS LOOP.
          int parentFolderID = this.getFolderIDFromFullPath(folderFullPath, mailAccountVO.getAccountID(), cvdal);

          if (parentFolderID > 0) {
            MailFolderVO newFolderVO = new MailFolderVO();
            newFolderVO.setFolderName(currentFolder.getName());
            newFolderVO.setFolderType(MailFolderVO.USER_FOLDER_TYPE);
            newFolderVO.setEmailAccountID(mailAccountVO.getAccountID());
            newFolderVO.setParentID(parentFolderID);
            folderID = this.addEmailFolder(newFolderVO, false, cvdal);
          }else{
            // if the parent folder did not exist locally, then we need
            // to add this folder to the ArrayList problemFolders. After
            // we are finished processing the "folders" array once, we'll
            // then process the folders in "problemFolders" to try and
            // make sure we successfully sync all folders.
            HashMap probFolder = new HashMap();
            probFolder.put("fullName", remoteFullName);
            probFolder.put("name", currentFolder.getName());
            problemFolders.add(probFolder);
            continue;
          }
        }

        // Now get the messages from the server and store them locally
        Message[] messageArray = currentFolder.getMessages();

        FetchProfile profile = new FetchProfile();
        // These make us supa fast!
        profile.add(FetchProfile.Item.FLAGS);
        profile.add(UIDFolder.FetchProfileItem.UID);
        //profile.add(FetchProfile.Item.ENVELOPE);
        //profile.add(FetchProfile.Item.HEADERS);
        currentFolder.fetch(messageArray, profile);

        ArrayList transport = new ArrayList();
        transport.add(reprocessFolders);
        transport.add(validIDs);
        transport.add(messageIDs);
        // k += this.handleIMAPMessageArray(messageArray, currentFolder, folderID, mailAccountVO, transport, store, cvdal);
        reprocessFolders = (ArrayList)transport.get(0);
        validIDs = (ArrayList)transport.get(1);
        messageIDs = (ArrayList)transport.get(2);
      }

      // perhaps get the list of folders that couldn't be created,
      // and try creating them again? THIS CODE NEEDS TO BE ABSTRACTED
      // OUT INTO A METHOD, BECAUSE IT IS IDENTICAL TO SOME CODE AT
      // THE TOP OF THE ABOVE LOOP.
      Iterator problemIter = problemFolders.iterator();
      while (problemIter.hasNext()) {
        HashMap probFolder = (HashMap)problemIter.next();
        String folderFullPath = "/root/";
        folderFullPath += (String)probFolder.get("fullName");
        folderFullPath = folderFullPath.replaceAll((String)probFolder.get("name")+"$", "");
        folderFullPath = folderFullPath.replaceAll("/$", "");

        IMAPFolder newFolder = (IMAPFolder)store.getFolder(folderFullPath);

        int parentFolderID = this.getFolderIDFromFullPath(folderFullPath, mailAccountVO.getAccountID(), cvdal);

        if (parentFolderID > 0) {
          MailFolderVO newFolderVO = new MailFolderVO();
View Full Code Here

          String name = folderVO.getFolderFullName();
          if (name == null) {
            name = folderVO.getFolderName();
          }
          IMAPFolder folder = (IMAPFolder)store.getFolder(name);

          folder.open(IMAPFolder.READ_WRITE);
          Message msg = folder.getMessageByUID(Long.parseLong(messageVO.getMessageUID()));
          msg.setFlag(Flags.Flag.DELETED, true);
          folder.close(true);
          numberDeleted = this.deleteMessageLocally(individualID, messageID, cvdal);
        } catch(Exception e) {
          System.out.println("Exception thrown in deleteMessage()");
          e.printStackTrace();
          return(0);
View Full Code Here

  public static Map<String, Integer> getAllFolders(IMAPFolder entryFolder, boolean recursive)
    throws Exception {

    Map<String, Integer> result = new Hashtable<String, Integer>();

    IMAPFolder currFolder = entryFolder;

    if (currFolder.list().length > 0) {
      IMAPFolder[] folderList = (IMAPFolder[]) currFolder.list();

      // Iterate over all subfolder
      for (IMAPFolder folder : folderList) {
        if (folder.exists()) {
          try {
View Full Code Here

    try {
      IMAPSSLStore imapStore = new IMAPSSLStore(session, urlName);

      imapStore.connect();
      IMAPFolder startFolder;

      if (urlName.getFile() == null) {
        // There is no folder given
        startFolder = (IMAPFolder) imapStore.getDefaultFolder();
      } else {
        startFolder = (IMAPFolder) imapStore.getFolder(urlName.getFile());
      }

      // Find messages (if folder exist and could be openend)
      if (startFolder.exists()) {
        try {
          startFolder.open(Folder.READ_ONLY);
          Message[] msgs = startFolder.getMessages();
          for (int i = 0; i < msgs.length; i++) {
            MimeMessage message = (MimeMessage) msgs[i];
            // It's a message -> Add a index job
            addJob(folderUrl + "/message_" + startFolder.getUID(message), folderUrl, false, true, null);
          }
          startFolder.close(false);

        } catch (MessagingException messageEx) {
          mLog.debug("Could not open folder for reading but this is not an errror. Folder URL is " + folderUrl);
        }
      }
View Full Code Here

        URLName urlName = new URLName(originURLName.getProtocol(), originURLName.getHost(),
          originURLName.getPort(), folder, originURLName.getUsername(), originURLName.getPassword());
   
        IMAPSSLStore imapStore = new IMAPSSLStore(session, urlName);
        imapStore.connect();
        IMAPFolder currentFolder;

        if (urlName.getFile() == null) {
          // There is no folder given
          currentFolder = (IMAPFolder) imapStore.getDefaultFolder();
        } else {
          currentFolder = (IMAPFolder) imapStore.getFolder(urlName.getFile());
        }

        currentFolder.open(Folder.READ_WRITE);
        MimeMessage cplMessage = (MimeMessage) currentFolder.getMessageByUID(messageUID);
   
        if (cplMessage != null) {
          cplMessage.setFlag(Flags.Flag.SEEN, true);
          cplMessage.writeTo(baos);
          baos.flush();
          bytearrayMessage = baos.toByteArray();
          baos.close();
          mLastModifiedDate = cplMessage.getSentDate();

        }

        currentFolder.close(false);
        imapStore.close();      
       
      }

    } catch (Throwable thr) {
View Full Code Here

TOP

Related Classes of com.sun.mail.imap.IMAPFolder$ProtocolCommand

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.