Package com.sun.mail.imap

Examples of com.sun.mail.imap.IMAPStore


      String serverType = accountVO.getAccountType();

      if (serverType.equalsIgnoreCase(MailAccountVO.IMAP_TYPE)) {
        try {
          Session session = null;
          IMAPStore store = this.setupIMAPConnection(accountVO, session);
          MailFolderVO toFolderVO = getEmailFolder(newFolderID);
          MailFolderVO fromFolderVO = getEmailFolder(oldFolderID);

          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); //??
View Full Code Here


    String serverType = accountVO.getAccountType();

    if (serverType.equalsIgnoreCase(MailAccountVO.IMAP_TYPE)) {
      try {
        Session session = null;
        IMAPStore store = this.setupIMAPConnection(accountVO, session);
        MailFolderVO toFolderVO = getEmailFolder(newFolderID);
        MailFolderVO fromFolderVO = getEmailFolder(oldFolderID);

        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);
View Full Code Here

    properties.put("mail.debug", "true");

    // TODO: Add the secure connection stuff to the IMAP fetchmail
    // Look at http://www.javaworld.com/javatips/jw-javatip115_p.html
    Session session = Session.getDefaultInstance(properties);
    IMAPStore store = (IMAPStore)session.getStore(mailAccountVO.getAccountType());
    store.connect(mailAccountVO.getMailServer(), mailAccountVO.getLogin(), mailAccountVO.getPassword());

    Folder defaultIMAPFolder =  null;
    IMAPFolder[] folders = null;
    try {
      defaultIMAPFolder = store.getDefaultFolder();
      // This gets ALL folders in the user's namespace. This will return a "flat"
      // list of all folders and subfolders, which we can process with a simple
      // loop instead of using recursion.
      folders = (IMAPFolder[])defaultIMAPFolder.list("*");
    } catch (MessagingException e1) {
      store.close();
    }

    // Check wheather the Account is view as a Support Email Account
    boolean isSupportEmailAccount = false;
    isSupportEmailAccount = this.isSupportEmailAccount(mailAccountVO.getAccountID(),cvdal);

    // When we have moved a message locally, we create a case where
    // the local system and the server get out of sync. So we'll save
    // the parent folder in this temporary ArrayList, and we'll
    // RE-process that folder again after finishing the loop the first time.
    ArrayList reprocessFolders = new ArrayList();
    // List of valid message IDs.
    Collection validIDs = new ArrayList();
    ArrayList messageIDs = new ArrayList();

    try {
      // Since the list of folders is returned from the server in
      // alphabetical order, there is the possibility that we'll get
      // a child folder before the parent folder. That causes a case
      // where we can't create that folder locally, so we'll stick
      // that folder in this temporary ArrayList, and process it again
      // after looping through all the folders the first time.
      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();
          newFolderVO.setFolderName((String)probFolder.get("name"));
          newFolderVO.setFolderType(MailFolderVO.USER_FOLDER_TYPE);
          newFolderVO.setEmailAccountID(mailAccountVO.getAccountID());
          newFolderVO.setParentID(parentFolderID);
          int folderID = this.addEmailFolder(newFolderVO, false, cvdal);
        }
      }
      // Delete any message that weren't on the server (which means they
      // were deleted elsewhere (or copied somewhere).
      this.deleteInvalidMessages(mailAccountVO.getOwnerID(), mailAccountVO.getAccountID(), validIDs, -1, cvdal);

      if (messageIDs.size() > 0) {
        this.applyRules(mailAccountVO.getAccountID(), messageIDs, cvdal);
      }
    }catch(Exception e){
      logger.error("[getIMAPMail]: Exception", e);
    }finally{
      //make sure each folder has been closed
      try {
        for (int i=0; i<folders.length; i++) {
          if (folders[i].isOpen()) {
            folders[i].close(true);
          }
        }
        store.close();
      } catch (Exception e) { }
      cvdal.destroy();
      cvdal = null;
    }

View Full Code Here

          MailMessageVO messageVO = this.getEmailMessageVO(individualID, messageID);
          MailAccountVO accountVO = this.getMailAccountVO(messageVO.getEmailAccountID());
          int folderID = messageVO.getEmailFolderID();

          Session session = null;
          IMAPStore store = this.setupIMAPConnection(accountVO, session);
          MailFolderVO folderVO = getEmailFolder(folderID);

          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);
View Full Code Here

   * @param accountVO
   */
  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

   * Drop in for new IMAP check all.
   */
  private void newIMAP(MailAccountVO accountVO)
  {
    CVDal cvdal = new CVDal(this.dataSource);
    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 {
      try {
        store.close();
      } catch (Exception e) {}
      cvdal.destroy();
    }
  }
View Full Code Here

  private IMAPStore setupIMAPConnection(MailAccountVO accountVO, Session session) throws NoSuchProviderException, MessagingException
  {
    Properties props = new Properties();
    //props.put("mail.debug", "true");
    session = Session.getDefaultInstance(props, null);
    IMAPStore store = (IMAPStore)session.getStore(accountVO.getAccountType());
    store.connect(accountVO.getMailServer(), accountVO.getLogin(), accountVO.getPassword());
    return(store);
  }
View Full Code Here

     * @return true if the store is an IMAP store and it has the store capability
     * @throws MessagingException In case capability check fails
     */
    private static boolean hasSortCapability(Store store) throws MessagingException {
        if (store instanceof IMAPStore) {
            IMAPStore imapStore = (IMAPStore) store;
            if (imapStore.hasCapability("SORT*")) {
                return true;
            }
        }
        return false;
    }
View Full Code Here

  /**
   *
   */
  private IMAPStore createStore() {

    IMAPStore store = null;
    try {
      store = (IMAPStore) session.getStore();
    }
    catch (NoSuchProviderException e) {
      logger.error( e.getMessage(), e );
View Full Code Here

  /**
   *
   */
  private IMAPStore createStore() {

    IMAPStore store = null;
    try {
      store = (IMAPStore) session.getStore();
    }
    catch (NoSuchProviderException e) {
      log.error( e.getMessage(), e );
View Full Code Here

TOP

Related Classes of com.sun.mail.imap.IMAPStore

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.