Package com.sun.mail.pop3

Examples of 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

        pop3Props.setProperty("mail.pop3.socketFactory.port", portNumber);

        URLName url = new URLName("pop3", host, port, "",username, password);

        Session session = Session.getInstance(pop3Props, null);
        Store store = new POP3SSLStore(session, url);
        store.connect();

        Folder folder = store.getFolder("INBOX");

        folder.open(Folder.READ_WRITE);

        Message[] msgs = folder.getMessages();

        if(msgs.length < 1){
          return emails;
        }

        // Use a suitable FetchProfile
        FetchProfile fp = new FetchProfile();
        fp.add(FetchProfile.Item.CONTENT_INFO);
        folder.fetch(msgs, fp);

        for (int i = 0; i < msgs.length; i++) {
          Message msg = msgs[i];
          Map<String, Object> email = new HashMap<String, Object>();
            getEmailContents(msg,email);

            // FROM
            if ((msg.getFrom()) != null) {
              email.put("From", getDataBetweenAngBrkts(msg.getFrom()[0].toString()));
            }

            // TO
            if ((msg.getRecipients(Message.RecipientType.TO)) != null) {
              String to = "";
              for (int j = 0; j < msg.getRecipients(RecipientType.TO).length; j++) {
                to += getDataBetweenAngBrkts(msg.getRecipients(RecipientType.TO)[j].toString())+",";
        }
              email.put("To", to.substring(0, to.length()-1));
            }

            // SUBJECT
            email.put("Subject", getCleanSubject(msg.getSubject()));

            // DATE
            Date d = msg.getSentDate();
            if(d != null){
              SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
          String d2 = sdf2.format(d);
          email.put("Date", d2.toString());
            }

            // CC
            if(msg.getRecipients(RecipientType.CC) != null){
              String cc = "";
              for (int j = 0; j < msg.getRecipients(RecipientType.CC).length; j++) {
          cc += getDataBetweenAngBrkts(msg.getRecipients(RecipientType.CC)[j].toString())+",";
        }
              email.put("Cc", cc.substring(0, cc.length()-1));
            }
           
            // BCC
            if(msg.getRecipients(RecipientType.BCC) != null){
              String bcc = "";
              for (int j = 0; j < msg.getRecipients(RecipientType.BCC).length; j++) {
          bcc += getDataBetweenAngBrkts(msg.getRecipients(RecipientType.BCC)[j].toString())+",";
        }
              email.put("Bcc", bcc.substring(0, bcc.length()-1));
            }

            // MESSAGE-ID
            //email.put("Message-ID",msg.getHeader("Message-ID")[0]);
           
            @SuppressWarnings("unchecked")
      Enumeration<Header> headers = msg.getAllHeaders();
            while(headers.hasMoreElements()){
              Header header = headers.nextElement();
             
              if(header.getName().equals("Message-ID"))
                  email.put("Message-ID", getDataBetweenAngBrkts(header.getValue()));
             
              if(header.getName().equals("In-Reply-To"))
              email.put("In-Reply-To", getDataBetweenAngBrkts(header.getValue()));
             
              if(header.getName().equals("References"))
              email.put("References", header.getValue());             
            }

            emails.add(email);
            msg.setFlag(Flag.DELETED, true);
        }

        folder.close(true);

        store.close();

        return emails;
  }
View Full Code Here

  }

  @Override
  protected Store getStore(Session session) throws NoSuchProviderException {
    URLName url = new URLName(PROTOCOL_POP3, host, port, "", username, password);
    return new POP3SSLStore(session, url);
  }
View Full Code Here

    public POP3RemoteStore(Session session, URLName url) {
  super(session, url);
    }

    protected Store getRemoteStore(Session session, URLName url) {
  return new POP3Store(session, url);
    }
View Full Code Here

TOP

Related Classes of 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.