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.PrintCommandListener;
24  import org.apache.commons.net.imap.IMAPClient;
25  
26  /**
27   * This is an example program demonstrating how to use the IMAP[S]Client class. This program connects to a IMAP[S] server, lists its capabilities and shows the
28   * status of the Inbox.
29   * <p>
30   * Usage: IMAPMail imap[s]://user:password@server/
31   * <p>
32   * For example<br>
33   * IMAPMail imaps://user:password@imap.mail.yahoo.com/<br>
34   * or<br>
35   * IMAPMail imaps://user:password@imap.googlemail.com/
36   */
37  public final class IMAPMail {
38  
39      public static void main(final String[] args) throws IOException {
40          if (args.length != 1) {
41              System.err.println("Usage: IMAPMail imap[s]://user:password@server/");
42              System.err.println("Connects to server; lists capabilities and shows Inbox status");
43              System.exit(1);
44          }
45  
46          final URI uri = URI.create(args[0]);
47  
48          // Connect and login
49          final IMAPClient imap = IMAPUtils.imapLogin(uri, 10000, null);
50  
51          // suppress login details
52          imap.addProtocolCommandListener(new PrintCommandListener(System.out, true));
53  
54          try {
55              imap.setSoTimeout(6000);
56  
57              imap.capability();
58  
59              imap.select("inbox");
60  
61              imap.examine("inbox");
62  
63              imap.status("inbox", new String[] { "MESSAGES" });
64  
65              imap.list("", "*"); // Show the folders
66  
67          } catch (final IOException e) {
68              System.out.println(imap.getReplyString());
69              e.printStackTrace();
70              System.exit(10);
71          } finally {
72              imap.logout();
73              imap.disconnect();
74          }
75      }
76  }
77  
78