IMAPUtils.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.apache.commons.net.examples.mail;

  18. import java.io.IOException;
  19. import java.net.URI;

  20. import org.apache.commons.net.ProtocolCommandListener;
  21. import org.apache.commons.net.imap.IMAPClient;
  22. import org.apache.commons.net.imap.IMAPSClient;

  23. /**
  24.  * Utility class for shared IMAP utilities
  25.  */
  26. final class IMAPUtils {

  27.     /**
  28.      * Parses the URI and use the details to connect to the IMAP(S) server and login.
  29.      *
  30.      * @param uri            the URI to use, e.g. imaps://user:pass@imap.mail.yahoo.com/folder or imaps://user:pass@imap.googlemail.com/folder
  31.      * @param defaultTimeout initial timeout (in milliseconds)
  32.      * @param listener       for tracing protocol IO (may be null)
  33.      * @return the IMAP client - connected and logged in
  34.      * @throws IOException if any problems occur
  35.      */
  36.     static IMAPClient imapLogin(final URI uri, final int defaultTimeout, final ProtocolCommandListener listener) throws IOException {
  37.         final String userInfo = uri.getUserInfo();
  38.         if (userInfo == null) {
  39.             throw new IllegalArgumentException("Missing userInfo details");
  40.         }

  41.         final String[] userPassword = userInfo.split(":");
  42.         if (userPassword.length != 2) {
  43.             throw new IllegalArgumentException("Invalid userInfo details: '" + userInfo + "'");
  44.         }

  45.         final String user = userPassword[0];
  46.         String password = userPassword[1];
  47.         // prompt for the password if necessary
  48.         password = Utils.getPassword(user, password);

  49.         final IMAPClient imap;

  50.         final String scheme = uri.getScheme();
  51.         if ("imaps".equalsIgnoreCase(scheme)) {
  52.             System.out.println("Using secure protocol");
  53.             imap = new IMAPSClient(true); // implicit
  54.         } else if ("imap".equalsIgnoreCase(scheme)) {
  55.             imap = new IMAPClient();
  56.         } else {
  57.             throw new IllegalArgumentException("Invalid protocol: " + scheme);
  58.         }
  59.         final int port = uri.getPort();
  60.         if (port != -1) {
  61.             imap.setDefaultPort(port);
  62.         }

  63.         imap.setDefaultTimeout(defaultTimeout);

  64.         if (listener != null) {
  65.             imap.addProtocolCommandListener(listener);
  66.         }

  67.         final String server = uri.getHost();
  68.         System.out.println("Connecting to server " + server + " on " + imap.getDefaultPort());

  69.         try {
  70.             imap.connect(server);
  71.             System.out.println("Successfully connected");
  72.         } catch (final IOException e) {
  73.             throw new IOException("Could not connect to server.", e);
  74.         }

  75.         if (!imap.login(user, password)) {
  76.             imap.disconnect();
  77.             throw new IOException("Could not login to server. Check login details.");
  78.         }

  79.         return imap;
  80.     }
  81. }