Ejemplo n.º 1
0
  /* (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;
  }