View Javadoc
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  
18  package org.apache.commons.net.examples.mail;
19  
20  import java.io.IOException;
21  import java.net.URI;
22  
23  import org.apache.commons.net.ProtocolCommandListener;
24  import org.apache.commons.net.imap.IMAPClient;
25  import org.apache.commons.net.imap.IMAPSClient;
26  
27  /**
28   * Utility class for shared IMAP utilities
29   */
30  class IMAPUtils {
31  
32      /**
33       * Parses the URI and use the details to connect to the IMAP(S) server and login.
34       *
35       * @param uri            the URI to use, e.g. imaps://user:pass@imap.mail.yahoo.com/folder or imaps://user:pass@imap.googlemail.com/folder
36       * @param defaultTimeout initial timeout (in milliseconds)
37       * @param listener       for tracing protocol IO (may be null)
38       * @return the IMAP client - connected and logged in
39       * @throws IOException if any problems occur
40       */
41      static IMAPClient imapLogin(final URI uri, final int defaultTimeout, final ProtocolCommandListener listener) throws IOException {
42          final String userInfo = uri.getUserInfo();
43          if (userInfo == null) {
44              throw new IllegalArgumentException("Missing userInfo details");
45          }
46  
47          final String[] userPassword = userInfo.split(":");
48          if (userPassword.length != 2) {
49              throw new IllegalArgumentException("Invalid userInfo details: '" + userInfo + "'");
50          }
51  
52          final String user = userPassword[0];
53          String password = userPassword[1];
54          // prompt for the password if necessary
55          password = Utils.getPassword(user, password);
56  
57          final IMAPClient imap;
58  
59          final String scheme = uri.getScheme();
60          if ("imaps".equalsIgnoreCase(scheme)) {
61              System.out.println("Using secure protocol");
62              imap = new IMAPSClient(true); // implicit
63          } else if ("imap".equalsIgnoreCase(scheme)) {
64              imap = new IMAPClient();
65          } else {
66              throw new IllegalArgumentException("Invalid protocol: " + scheme);
67          }
68          final int port = uri.getPort();
69          if (port != -1) {
70              imap.setDefaultPort(port);
71          }
72  
73          imap.setDefaultTimeout(defaultTimeout);
74  
75          if (listener != null) {
76              imap.addProtocolCommandListener(listener);
77          }
78  
79          final String server = uri.getHost();
80          System.out.println("Connecting to server " + server + " on " + imap.getDefaultPort());
81  
82          try {
83              imap.connect(server);
84              System.out.println("Successfully connected");
85          } catch (final IOException e) {
86              throw new IOException("Could not connect to server.", e);
87          }
88  
89          if (!imap.login(user, password)) {
90              imap.disconnect();
91              throw new IOException("Could not login to server. Check login details.");
92          }
93  
94          return imap;
95      }
96  }