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
22 import org.apache.commons.net.PrintCommandListener;
23 import org.apache.commons.net.imap.IMAPClient;
24 import org.apache.commons.net.imap.IMAPSClient;
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] server hostname> <username> <password> [secure protocol, e.g. TLS]
32 * <p>
33 */
34 public final class IMAPMail
35 {
36
37 public static void main(String[] args)
38 {
39 if (args.length < 3)
40 {
41 System.err.println(
42 "Usage: IMAPMail <imap server hostname> <username> <password> [TLS]");
43 System.exit(1);
44 }
45
46 String server = args[0];
47 String username = args[1];
48 String password = args[2];
49
50 String proto = (args.length > 3) ? args[3] : null;
51
52 IMAPClient imap;
53
54 if (proto != null) {
55 System.out.println("Using secure protocol: " + proto);
56 imap = new IMAPSClient(proto, true); // implicit
57 // enable the next line to only check if the server certificate has expired (does not check chain):
58 // ((IMAPSClient) imap).setTrustManager(TrustManagerUtils.getValidateServerCertificateTrustManager());
59 // OR enable the next line if the server uses a self-signed certificate (no checks)
60 // ((IMAPSClient) imap).setTrustManager(TrustManagerUtils.getAcceptAllTrustManager());
61 } else {
62 imap = new IMAPClient();
63 }
64 System.out.println("Connecting to server " + server + " on " + imap.getDefaultPort());
65
66 // We want to timeout if a response takes longer than 60 seconds
67 imap.setDefaultTimeout(60000);
68
69 // suppress login details
70 imap.addProtocolCommandListener(new PrintCommandListener(System.out, true));
71
72 try
73 {
74 imap.connect(server);
75 }
76 catch (IOException e)
77 {
78 throw new RuntimeException("Could not connect to server.", e);
79 }
80
81 try
82 {
83 if (!imap.login(username, password))
84 {
85 System.err.println("Could not login to server. Check password.");
86 imap.disconnect();
87 System.exit(3);
88 }
89
90 imap.setSoTimeout(6000);
91
92 imap.capability();
93
94 imap.select("inbox");
95
96 imap.examine("inbox");
97
98 imap.status("inbox", new String[]{"MESSAGES"});
99
100 imap.logout();
101 imap.disconnect();
102 }
103 catch (IOException e)
104 {
105 System.out.println(imap.getReplyString());
106 e.printStackTrace();
107 System.exit(10);
108 return;
109 }
110 }
111 }
112
113 /* kate: indent-width 4; replace-tabs on; */