/* (non-Javadoc)
   * @see org.claros.commons.mail.protocols.FetchProtocol#connect(int)
   */
  public ConnectionMetaHandler connect(int connectType)
      throws SystemException, ConnectionException, ServerDownException {
    try {
      try {
        disconnect();
        try {
          Thread.sleep(2000);
        } catch (Exception k) {
        }
      } catch (Exception k) {
      }

      if (handler == null || !handler.getStore().isConnected()) {
        Properties props = new Properties();

        if (profile.getFetchSSL() != null && profile.getFetchSSL().toLowerCase().equals("true")) {
          Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

          Security.setProperty(
              "ssl.SocketFactory.provider",
              "org.claros.commons.mail.protocols.DummySSLSocketFactory");
          props.setProperty("mail.store.protocol", "pop3");
          props.setProperty("mail.pop3.host", profile.getFetchServer());
          props.setProperty("mail.pop3.port", profile.getFetchPort());

          props.setProperty(
              "mail.pop3.socketFactory.class",
              "org.claros.commons.mail.protocols.DummySSLSocketFactory");
          props.setProperty("mail.pop3.socketFactory.fallback", "false");
          props.setProperty("mail.pop3.port", profile.getFetchPort());
          props.setProperty("mail.pop3.socketFactory.port", profile.getFetchPort());
        }

        Session session = Session.getInstance(props);
        handler = new ConnectionMetaHandler();
        handler.setStore(session.getStore(profile.getProtocol()));
        handler
            .getStore()
            .connect(
                profile.getFetchServer(),
                profile.getIFetchPort(),
                auth.getUsername(),
                auth.getPassword());
        handler.setMbox(handler.getStore().getDefaultFolder());
        handler.setMbox(handler.getMbox().getFolder(Constants.FOLDER_INBOX(profile)));
        handler.getMbox().open(connectType);

        // storing the folder in map
        pop3Folders.put(auth.getUsername(), handler.getMbox());

        handler.setTotalMessagesCount(handler.getMbox().getMessageCount());
      }
    } catch (AuthenticationFailedException e) {
      System.out.println(
          "Pop3 Mailbox was busy with another session and there is a read write lock. A few minutes later when the lock is released everything will be fine.");
    } catch (NoSuchProviderException e) {
      System.out.println(profile.getProtocol() + " provider could not be found.");
      throw new SystemException(e);
    } catch (MessagingException e) {
      System.out.println("Connection could not be established.");
      throw new ConnectionException(e);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return handler;
  }
Example #2
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;
  }