Package com.google.code.com.sun.mail.pop3

Examples of com.google.code.com.sun.mail.pop3.POP3Folder


    Session session = Session.getDefaultInstance(properties);
    Store store = session.getStore(mailAccountVO.getAccountType());

    store.connect(mailAccountVO.getMailServer(), mailAccountVO.getLogin(), mailAccountVO.getPassword());

    POP3Folder folder = null;
    Message[] messageArray = null;
    try {
      folder = (POP3Folder)store.getFolder("INBOX");

      // open the remote folder for reading (and writing)
      folder.open(Folder.READ_WRITE);

      // get all the messages from the remote folder
      messageArray = folder.getMessages();
    }catch(MessagingException e){
      try {folder.close(mailAccountVO.isLeaveMessagesOnServer());} catch (Exception ex) {}
      store.close();
    }
    CVDal cvdal = new CVDal(this.dataSource);

    ArrayList messageIDs = new ArrayList();

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

    try {
      // Get list of UID's from database for this account
      ArrayList uidList = this.getUidList(mailAccountVO.getAccountID(), cvdal);

      // this will hold all the UIDs that are left on the server, so
      // that we can store them and not download them next time
      ArrayList serverUIDs = new ArrayList();

      // loop through the array of Message objects, and process each
      for (int i=0; i<messageArray.length; i++) {
        // get the unique ID of the message
        String messageUID = folder.getUID(messageArray[i]);

        HashMap embededImageMap = new HashMap();
        // if the uid List in the database does not contain the UID
        // of the current message, then we need to download this message.
        // Note that if uid list is empty, we'll download all messages
        if (! uidList.contains(messageUID)) {
          // save message in database, then save attachments
          int newMessageID = this.addMessage(messageArray[i], messageUID, mailAccountVO.getAccountID(), mailAccountVO.getOwnerID(), defaultFolder.getFolderID(), cvdal);
          this.saveAttachments(this.getPartContent(messageArray[i]), newMessageID, mailAccountVO.getAccountID(), mailAccountVO.getOwnerID(), attachmentFolderID, cvdal, embededImageMap);
          this.addBody(messageArray[i], newMessageID, embededImageMap, cvdal);

          embededImageMap = null;


          // Add the newMessageID to the messageIDs Collection which will
          // be used later by the Rules framework and the Support framework
          messageIDs.add(new Integer(newMessageID));
          newMessages++;
        }   // end if (addMessage)

        if (mailAccountVO.isLeaveMessagesOnServer()) {
          // need to add this UID to the local database, so that
          // we don't download it next time. No need to do so if
          // we're expunging messages from the server.
          serverUIDs.add(messageUID);
          messageArray[i].setFlag(Flags.Flag.SEEN, true);
        }else{
          // need to delete this message from the remote server
          messageArray[i].setFlag(Flags.Flag.DELETED, true);
        }
      }   // end for(int i=0; i<messageArray.length; i++)

      // cleanup uid list in database, adding new uids or deleting the ones that aren't on server anymore
      this.removeInvalidUIDs(mailAccountVO.getAccountID(), serverUIDs, cvdal);

      // if we have just downloaded any messages, we need to apply
      // any rules to the messages that we just downloaded. We'll
      // pass the list of messageIDs (from our database) to
      // this.applyRules() which will do all the rules stuff.
      if (messageIDs != null && messageIDs.size() > 0) {
        this.applyRules(mailAccountVO.getAccountID(), messageIDs, cvdal);
      }
    }finally{
      //Be a good programmer and clean up your connections.
      try { folder.close(! mailAccountVO.isLeaveMessagesOnServer());} catch (Exception e) {}
      try { store.close(); } catch (Exception e) {}
      cvdal.destroy();
      cvdal = null;
    }
View Full Code Here


        final Session session = Session.getDefaultInstance(props);
        session.setDebug(isDebug());
        final Store store = session.getStore(transport);
        store.connect(getLogin(request), getPassword(request));
        final POP3Folder inbox = (POP3Folder) store.getFolder("INBOX");
        inbox.open(Folder.READ_WRITE);
        final FetchProfile profile = new FetchProfile();
        profile.add(UIDFolder.FetchProfileItem.UID);
        final Message[] messages = inbox.getMessages();
        inbox.fetch(messages, profile);

        if ((path == null) || path.equals("") || path.equals("/")) {
            if (Method.GET.equals(request.getMethod())
                    || Method.HEAD.equals(request.getMethod())) {
                // Set the result document
                response.setEntity(createRepresentation(messages, inbox));
            } else {
                response.setStatus(Status.CLIENT_ERROR_METHOD_NOT_ALLOWED);
                response.getAllowedMethods().add(Method.GET);
                response.getAllowedMethods().add(Method.HEAD);
            }
        } else if (path.startsWith("/")) {
            // Retrieve the specified message
            final String mailUid = path.substring(1);
            Message message = null;

            for (int i = 0; (message == null) && (i < messages.length); i++) {
                final String uid = inbox.getUID(messages[i]);

                if (mailUid.equals(uid)) {
                    message = messages[i];
                }
            }

            if (message == null) {
                // Message not found
                response.setStatus(Status.CLIENT_ERROR_NOT_FOUND,
                        "No message matches the given UID: " + mailUid);
            } else {
                if (Method.GET.equals(request.getMethod())
                        || Method.HEAD.equals(request.getMethod())) {
                    // Set the result document
                    response.setEntity(createRepresentation(message));
                } else if (Method.DELETE.equals(request.getMethod())) {
                    message.setFlag(Flags.Flag.DELETED, true);
                    updateFolder = true;
                } else {
                    response.setStatus(Status.CLIENT_ERROR_METHOD_NOT_ALLOWED);
                    response.getAllowedMethods().add(Method.GET);
                    response.getAllowedMethods().add(Method.HEAD);
                    response.getAllowedMethods().add(Method.DELETE);
                }
            }
        }

        inbox.close(updateFolder);
        store.close();
    }
View Full Code Here

TOP

Related Classes of com.google.code.com.sun.mail.pop3.POP3Folder

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.