Пример #1
0
 /**
  * Log the name of a folder and it´s subfolders.
  *
  * @param aFolder the folder to show
  * @throws MessagingException if we cannot list the subfolders
  * @return a newline separated list of all folders
  */
 private List<String> listFolders(final Folder aFolder) throws MessagingException {
   List<String> retval = new LinkedList<String>();
   retval.add(aFolder.getFullName());
   LOG.info("folder: " + aFolder.getFullName());
   Folder[] subfolders = aFolder.list();
   for (Folder folder : subfolders) {
     retval.addAll(listFolders(folder));
   }
   return retval;
 }
  /**
   * Make a specific request to the underlying transport. Endpoint can be in the form of
   * pop3://username:[email protected]
   *
   * @param timeout the maximum time the operation should block before returning. The call should
   *     return immediately if there is data available. If no data becomes available before the
   *     timeout elapses, null will be returned
   * @return the result of the request wrapped in a MuleMessage object. Null will be returned if no
   *     data was avaialable
   * @throws Exception if the call to the underlying protocal causes an exception
   */
  @Override
  protected MuleMessage doRequest(long timeout) throws Exception {
    long t0 = System.currentTimeMillis();
    if (timeout < 0) {
      timeout = Long.MAX_VALUE;
    }

    do {
      if (hasMessages()) {
        int count = getMessageCount();
        if (count > 0) {
          Message message = getNextMessage();
          if (message != null) {
            // so we don't get the same message again
            flagMessage(message);

            if (moveToFolder != null) {
              Message newMessage = message;
              // If we're using IMAP we need to cache the message contents so the message is
              // accessible after the
              // folder is closed
              if (message instanceof IMAPMessage) {
                // We need to copy and cache so that the message cna be moved
                newMessage = new MimeMessage((IMAPMessage) message);
              }
              folder.copyMessages(new Message[] {message}, moveToFolder);
              message = newMessage;
            }
            return createMuleMessage(message, endpoint.getEncoding());
          }
        } else if (count == -1) {
          throw new MessagingException(
              "Cannot monitor folder: " + folder.getFullName() + " as folder is closed");
        }
      }

      long sleep =
          Math.min(
              castConnector().getCheckFrequency(), timeout - (System.currentTimeMillis() - t0));

      if (sleep > 0) {
        if (logger.isDebugEnabled()) {
          logger.debug("No results, sleeping for " + sleep);
        }
        try {
          Thread.sleep(sleep);
        } catch (InterruptedException e) {
          logger.warn(
              "Thread interrupted while requesting email on: "
                  + endpoint.getEndpointURI().toString());
          return null;
        }
      } else {

        logger.debug("Timeout");
        return null;
      }

    } while (true);
  }
Пример #3
0
  /**
   * Endpoint can be in the form of pop3://username:[email protected]
   *
   * @param endpointUri
   * @param timeout
   * @return
   * @throws Exception
   */
  public UMOMessage receive(UMOEndpointURI endpointUri, long timeout) throws Exception {
    initialise(endpointUri);

    long t0 = System.currentTimeMillis();
    if (timeout < 0) {
      timeout = Long.MAX_VALUE;
    }
    do {
      int count = folder.getMessageCount();
      if (count > 0) {
        Message message = folder.getMessage(1);
        // so we don't get the same message again
        message.setFlag(Flags.Flag.DELETED, true);
        return new MuleMessage(connector.getMessageAdapter(message));
      } else if (count == -1) {
        throw new MessagingException(
            "Cannot monitor folder: " + folder.getFullName() + " as folder is closed");
      }
      long sleep =
          Math.min(this.connector.getCheckFrequency(), timeout - (System.currentTimeMillis() - t0));
      if (sleep > 0) {
        if (logger.isDebugEnabled()) {
          logger.debug("No results, sleeping for " + sleep);
        }
        Thread.sleep(sleep);
      } else {
        logger.debug("Timeout");
        return null;
      }
    } while (true);
  }
Пример #4
0
  public void updateFolders() throws PortalException {
    if (_log.isDebugEnabled()) {
      _log.debug("Updating folders");
    }

    List<javax.mail.Folder> jxFolders = _imapAccessor.getFolders();

    for (javax.mail.Folder jxFolder : jxFolders) {
      try {
        FolderLocalServiceUtil.getFolder(account.getAccountId(), jxFolder.getFullName());
      } catch (NoSuchFolderException nsfe) {
        FolderLocalServiceUtil.addFolder(
            user.getUserId(),
            account.getAccountId(),
            jxFolder.getFullName(),
            jxFolder.getName(),
            0);
      }
    }

    long draftFolderId = account.getDraftFolderId();
    long inboxFolderId = account.getInboxFolderId();
    long sentFolderId = account.getSentFolderId();
    long trashFolderId = account.getTrashFolderId();

    if (draftFolderId <= 0) {
      draftFolderId = getFolderId("draft");
    }

    if (inboxFolderId <= 0) {
      inboxFolderId = getFolderId("inbox");
    }

    if (sentFolderId <= 0) {
      sentFolderId = getFolderId("sent");
    }

    if (sentFolderId <= 0) {
      sentFolderId = getFolderId("sent-mail");
    }

    if (trashFolderId <= 0) {
      trashFolderId = getFolderId("trash");
    }

    updateFolders(inboxFolderId, draftFolderId, sentFolderId, trashFolderId);
  }
Пример #5
0
  public int handleFirstTime(
      Account account,
      FolderHelper folderHelper,
      int folderDepth,
      String messageStoreBucket,
      Set<String> storedMessageIds,
      String context,
      StopWatch watch)
      throws Exception {
    int newMessages = 0;
    Date lastMessageReceivedDate = null;
    log.info(String.format("handling first mailcheck for %s", context));

    Folder folder;
    while ((folder = folderHelper.next()) != null) {
      String folderContext = context + " ,folderName=" + folder.getFullName();
      IMAPFolder imapFolder = (IMAPFolder) folder;

      Collection<Integer> messages = getFirstTimeMessages(imapFolder, folderContext);

      if (messages.size() > 0) {
        storedMessageIds = new HashSet<String>();

        for (Integer messageNumber : messages) {
          Message message = imapFolder.getMessage(messageNumber);

          if (lastMessageReceivedDate == null
              || lastMessageReceivedDate.before(message.getReceivedDate())) {
            lastMessageReceivedDate = message.getReceivedDate();
          }

          String messageId = getId(account, imapFolder, message);

          if (processMessage(
              account,
              message,
              messageNumber,
              messageId,
              imapFolder,
              storedMessageIds,
              messageStoreBucket,
              folderContext,
              watch,
              folderHelper)) {
            newMessages++;
          }
        }
      }
    }
    updateAccount(account, null, 0, folderDepth, lastMessageReceivedDate);
    return newMessages;
  }
  protected void renameMailbox(
      final Properties props, final String folderName, final String user, final String password)
      throws MessagingException {
    final Store store = Session.getInstance(props).getStore();

    store.connect(user, password);
    checkStoreForTestConnection(store);
    final Folder f = store.getFolder(folderName);

    f.renameTo(store.getFolder("renamed_from_" + folderName));

    logger.info("Renamed " + f.getFullName());
    store.close();
  }
 /** @param folder */
 public void setFolder(Folder folder) {
   synchronized (folderLock) {
     if (folder == null) {
       throw new IllegalArgumentException("Mail folder cannot be null");
     }
     this.folder = folder;
     if (!this.folder.isOpen()) {
       try {
         this.folder.open(Folder.READ_WRITE);
       } catch (MessagingException e) {
         logger.warn("Failed to open folder: " + folder.getFullName(), e);
       }
     }
   }
 }
 protected void ensureFolderIsOpen(Folder fldr) {
   if (!fldr.isOpen()) {
     try {
       // Depending on Server implementation it's not always
       // necessary to open the folder to check it
       // Opening folders can be exprensive!
       fldr.open(Folder.READ_WRITE);
     } catch (MessagingException e) {
       logger.warn(
           "Failed to open folder: "
               + fldr.getFullName()
               + " This is not an exception since some server implementations do not require the folder to be open",
           e);
     }
   }
 }
  @Override
  public void poll() {
    boolean done = false;
    while (!done) {
      synchronized (folderLock) {
        if (getLifecycleState().isStopping() || getLifecycleState().isStopped()) {
          break;
        }
        try {
          try {
            if (!folder.isOpen()) {
              folder.open(Folder.READ_WRITE);
            }
          } catch (Exception e) {
            if (logger.isDebugEnabled()) {
              logger.debug("ignoring exception: " + e.getMessage());
            }
          }

          int count = folder.getMessageCount();
          int batchSize = getBatchSize(count);
          if (count > 0) {
            Message[] messages = folder.getMessages(1, batchSize);
            MessageCountEvent event =
                new MessageCountEvent(folder, MessageCountEvent.ADDED, true, messages);
            messagesAdded(event);
          } else if (count == -1) {
            throw new MessagingException(
                "Cannot monitor folder: " + folder.getFullName() + " as folder is closed");
          }
          done = batchSize >= count;
        } catch (MessagingException e) {
          done = true;
          getConnector().getMuleContext().getExceptionListener().handleException(e);
        } finally {
          try {
            folder.close(true); // close and expunge deleted messages
          } catch (Exception e) {
            logger.error("Failed to close pop3  inbox: " + e.getMessage());
          }
        }
      }
    }
  }
  private void receiveMail() {
    String username = USERNAME;
    String password = PASSWORD;

    try {
      ServerConfigProvider provider = ServerConfigFactory.fromEmailAdress(username);
      MailReceiver mailReceiver = new MailReceiver(provider, new User(username, password));

      for (Folder folders : mailReceiver.getFolders()) {
        try {
          System.out.println(folders.getFullName() + " " + folders.getMessageCount());
        } catch (Exception e) {
          System.err.println(e.getMessage());
        }
      }

      Folder folder = mailReceiver.getFolderByName("Inbox");
      folder.open(Folder.READ_ONLY);
      for (Message message : folder.getMessages()) {
        List<File> attachments =
            MessageHelper.getAttachments(Environment.getExternalStorageDirectory(), message);
        if (!attachments.isEmpty()) {
          for (File attachment : attachments) {
            System.out.println(
                String.format(
                    "from %s subject %s attachmentname %s",
                    message.getFrom()[0], message.getSubject(), attachment.getName()));
          }
        } else {
          System.out.println(
              String.format(
                  "from %s subject %s no attachement", message.getFrom()[0], message.getSubject()));
        }
      }

      mailReceiver.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Пример #11
0
 public Folder next() {
   try {
     boolean hasMessages = false;
     Folder next;
     do {
       if (lastFolder != null) {
         lastFolder.close(false);
         lastFolder = null;
       }
       if (folders.isEmpty()) {
         mailbox.close();
         return null;
       }
       next = folders.remove(0);
       if (next != null) {
         String fullName = next.getFullName();
         if (!excludeFolder(fullName)) {
           hasMessages = (next.getType() & Folder.HOLDS_MESSAGES) != 0;
           next.open(Folder.READ_ONLY);
           lastFolder = next;
           LOG.info("Opened folder: {}", fullName);
         }
         if (((next.getType() & Folder.HOLDS_FOLDERS) != 0)) {
           Folder[] children = next.list();
           LOG.debug("Adding its children to list");
           for (int i = children.length - 1; i >= 0; i--) {
             folders.add(0, children[i]);
             LOG.debug("Child name: {}", children[i].getFullName());
           }
           if (children.length == 0) {
             LOG.debug("No children");
           }
         }
       }
     } while (!hasMessages);
     return next;
   } catch (MessagingException e) {
     return null;
   }
 }
Пример #12
0
  public boolean jumpToFolder(String folderName) {
    FolderIterator newFolderIter = null;
    MessageIterator newMsgIter = null;
    try {
      newFolderIter = new FolderIterator(mailbox);

      while (newFolderIter.hasNext()) {
        Folder next = newFolderIter.next();
        if (folderName.equals(next.getFullName())) {
          newMsgIter = new MessageIterator(next, batchSize);
          folderIter = newFolderIter;
          msgIter = newMsgIter;
          return true;
        }
      }

      return false;
    } catch (EmailFetchException e) {
      LOG.error("Fetching email failed", e);
      return false;
    }
  }
Пример #13
0
  protected void initialise(UMOEndpointURI endpoint) throws MessagingException {
    if (!initialised.get()) {
      String inbox = null;
      if (connector.getProtocol().equals("imap") && endpoint.getParams().get("folder") != null) {
        inbox = (String) endpoint.getParams().get("folder");
      } else {
        inbox = Pop3Connector.MAILBOX;
      }

      URLName url =
          new URLName(
              endpoint.getScheme(),
              endpoint.getHost(),
              endpoint.getPort(),
              inbox,
              endpoint.getUsername(),
              endpoint.getPassword());

      session = MailUtils.createMailSession(url);
      session.setDebug(logger.isDebugEnabled());

      Store store = session.getStore(url);
      store.connect();
      folder = store.getFolder(inbox);
      if (!folder.isOpen()) {
        try {
          // Depending on Server implementation it's not always
          // necessary
          // to open the folder to check it
          // Opening folders can be exprensive!
          // folder.open(Folder.READ_ONLY);
          folder.open(Folder.READ_WRITE);
        } catch (MessagingException e) {
          logger.warn("Failed to open folder: " + folder.getFullName(), e);
        }
      }
    }
  }
Пример #14
0
  public Quota getQuota() throws MessagingException {
    Quota[] quotas = store.getQuota(folder.getFullName());

    if (quotas != null && quotas.length > 0) return quotas[0];
    else return null;
  }
Пример #15
0
  @Override
  protected Iterator<MultiValueHashMap<String, Object>> getSubDataEntitiesInformation(
      InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context)
      throws Exception {

    // imap url schema: imap[s]://uname@hostname:port/folder;uidvalidity=385759045/;uid=20. Examples
    // (incl. message-referenzierung)
    // http://xml.resource.org/public/rfc/html/rfc2192.html#anchor10
    // allerdings nimmt der Java ImapStore auch URLs mit Passwörtern an. Dann geht auch
    // imap[s]://uname:pwd@hostname:port/folder;uidvalidity=385759045/;uid=20

    CrawlerContext crawlerContext = context.get(CrawlerContext.class, new CrawlerContext());

    String strContainerURL = metadata.get(Metadata.SOURCE);

    URLName containerURLName = new URLName(strContainerURL);

    if (m_mailStore == null) m_mailStore = connect2Server(containerURLName, context);

    // wenn kein directory angegeben wird, dann crawlen wir einfach den default folder und die inbox
    LinkedList<Folder> llFolderz2Crawl = new LinkedList<Folder>();
    if (containerURLName.getFile() != null) {
      Folder folder = m_mailStore.getFolder(containerURLName.getFile());
      if (folder != null && folder.exists()) llFolderz2Crawl.add(folder);
      else throw new FileNotFoundException("Can't find imap folder '" + folder.getFullName() + "'");

    } else {
      Folder folder = m_mailStore.getDefaultFolder();
      if (folder != null && folder.exists()) llFolderz2Crawl.add(folder);

      folder = m_mailStore.getFolder("INBOX");
      if (folder != null && folder.exists()) llFolderz2Crawl.add(folder);
    }

    LinkedList<MultiValueHashMap<String, Object>> llEntityInfo =
        new LinkedList<MultiValueHashMap<String, Object>>();

    for (Folder folder2crawl : llFolderz2Crawl) {
      // Jetzt haben wir die Containerobjekte - nun geben wir die Daten zu den SubEntities zurück

      // die subfolder
      boolean bFolderCanHaveSubFolders =
          (folder2crawl.getType() & Folder.HOLDS_FOLDERS) == Folder.HOLDS_FOLDERS;

      if (bFolderCanHaveSubFolders) {
        folder2crawl.open(Folder.READ_ONLY);

        Folder[] subFolders = folder2crawl.list();
        for (Folder subFolder : subFolders) {
          URLName urlName = subFolder.getURLName();
          URLName urlNameWithPassword =
              new URLName(
                  containerURLName.getProtocol(),
                  urlName.getHost(),
                  urlName.getPort(),
                  urlName.getFile(),
                  urlName.getUsername(),
                  containerURLName.getPassword());

          if (!checkIfInConstraints(urlName.toString(), null, context)) continue;

          MultiValueHashMap<String, Object> hsEntityInformation =
              new MultiValueHashMap<String, Object>();

          hsEntityInformation.add(CrawlerParser.SOURCEID, urlName);
          hsEntityInformation.add("urlNameWithPassword", urlNameWithPassword);
          hsEntityInformation.add("folder", subFolder.getFullName());

          llEntityInfo.add(hsEntityInformation);
        }
      }

      // die messages
      boolean bFolderCanHaveMessages =
          (folder2crawl.getType() & Folder.HOLDS_MESSAGES) == Folder.HOLDS_MESSAGES;

      if (bFolderCanHaveMessages) {
        if (!folder2crawl.isOpen()) folder2crawl.open(Folder.READ_ONLY);

        // wir holen uns alle nicht-deleted messages, und werfen noch die raus, die 'expunged' sind
        Message[] relevantMessagesOfFolder =
            folder2crawl.search(new FlagTerm(new Flags(Flags.Flag.DELETED), false));
        ArrayList<Message> nonDelNonExpungedMessages = new ArrayList<Message>();
        for (Message message : relevantMessagesOfFolder)
          if (!message.isExpunged()) nonDelNonExpungedMessages.add(message);
        relevantMessagesOfFolder = nonDelNonExpungedMessages.toArray(new Message[0]);

        // die Daten die wir später benötigen holen wir uns effizient in einem Rutsch - deswegen
        // benötigen wir auch keinen Thread mit dem
        // OneAfterOneIterator, um Speicher zu sparen (siehe DirectoryCrawlerParser). Das Array
        // haben wir hier eh. Entweder oder.
        FetchProfile profile = new FetchProfile();
        profile.add(UIDFolder.FetchProfileItem.UID);
        profile.add("Message-ID");
        folder2crawl.fetch(relevantMessagesOfFolder, profile);

        for (int i = 0;
            i < relevantMessagesOfFolder.length && !crawlerContext.stopRequested();
            i++) {
          MimeMessage message = (MimeMessage) relevantMessagesOfFolder[i];

          // hier brauchen wir noch eine URL mit und eine ohne Passwort
          URLName urlName = getMessageUrl(folder2crawl, message);
          URLName urlNameWithPassword =
              new URLName(
                  containerURLName.getProtocol(),
                  urlName.getHost(),
                  urlName.getPort(),
                  urlName.getFile(),
                  urlName.getUsername(),
                  containerURLName.getPassword());

          if (!checkIfInConstraints(urlName.toString(), message, context)) continue;

          MultiValueHashMap<String, Object> hsEntityInformation =
              new MultiValueHashMap<String, Object>();

          hsEntityInformation.add(CrawlerParser.SOURCEID, urlName);
          hsEntityInformation.add("urlNameWithPassword", urlNameWithPassword);
          hsEntityInformation.add("Message-ID", message.getHeader("Message-ID")[0]);
          hsEntityInformation.add("folder", folder2crawl.getFullName());

          llEntityInfo.add(hsEntityInformation);
        }
      }

      // wir haben die folder abgearbeitet, dann können wir diesen Speicher wieder frei geben
      m_hsImapFolder2Stickyness.clear();

      if (folder2crawl.isOpen()) folder2crawl.close(false);
    }

    return llEntityInfo.iterator();
  }
Пример #16
0
  /* (non-Javadoc)
   * @see org.claros.commons.mail.protocols.Protocol#connect(int)
   */
  public ConnectionMetaHandler connect(int connectType)
      throws SystemException, ConnectionException, ServerDownException {
    Folder fold = null;
    try {
      if (handler == null || handler.getStore() == null || !handler.getStore().isConnected()) {
        Properties props = new Properties();
        //				props.setProperty("mail.imap.separatestoreconnection", "true");
        //				props.setProperty("mail.imap.connectionpoolsize", "0");
        //				props.setProperty("mail.imap.connectionpooltimeout", "100");

        //				props.setProperty("mail.debug", "true");
        Session session = Session.getDefaultInstance(props);
        log.debug("session instance initiated");
        handler = new ConnectionMetaHandler();
        handler.setStore(session.getStore(profile.getProtocol()));
        log.debug("session store set. protocol is: " + profile.getProtocol());
        handler
            .getStore()
            .connect(
                profile.getFetchServer(),
                profile.getIFetchPort(),
                auth.getUsername(),
                auth.getPassword());
        if (handler.getStore().isConnected()) {
          log.debug("Store has been connected... Successful");
        } else {
          log.warn("Connection unsuccessfull...!!");
        }
      }
      fold = handler.getStore().getFolder(Constants.FOLDER_INBOX(profile));

      HashMap imapUserFolders = (HashMap) imapFolders.get(auth.getUsername());
      imapUserFolders.put("INBOX", fold);
      imapFolders.put(auth.getUsername(), imapUserFolders);

      handler.setMbox(fold);
      log.debug("Got mailbox folder. Folder is: " + fold.getFullName());
      handler.setTotalMessagesCount(fold.getMessageCount());
      log.debug("Message Count:" + handler.getTotalMessagesCount());
    } catch (FolderNotFoundException e) {
      log.fatal(
          profile.getProtocol()
              + " cannot identify the INBOX folder. Please check your folder-namespace variable at config.xml.");
      throw new SystemException(e);
    } catch (NoSuchProviderException e) {
      log.fatal(profile.getProtocol() + " provider could not be found.");
      throw new SystemException(e);
    } catch (MessagingException e) {
      Exception ne = e.getNextException();
      if (ne != null) {
        if (ne instanceof ConnectException || ne instanceof IOException) {
          throw new ServerDownException("Server is unreachable.");
        }
      }
      log.error("Connection could not be established." + e.getMessage());
      //			throw new ConnectionException(e);
    } catch (Exception e) {
      log.error("An unknown exception while connect.", e);
    }
    return handler;
  }