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