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    *      https://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.BufferedReader;
21  import java.io.IOException;
22  import java.util.Locale;
23  
24  import org.apache.commons.net.examples.PrintCommandListeners;
25  import org.apache.commons.net.pop3.POP3Client;
26  import org.apache.commons.net.pop3.POP3MessageInfo;
27  import org.apache.commons.net.pop3.POP3SClient;
28  
29  /**
30   * This is an example program demonstrating how to use the POP3[S]Client class. This program connects to a POP3[S] server and retrieves the message headers of
31   * all the messages, printing the {@code From:} and {@code Subject:} header entries for each message.
32   * <p>
33   * See main() method for usage details
34   */
35  public final class POP3Mail {
36  
37      public static void main(final String[] args) {
38          if (args.length < 3) {
39              System.err.println("Usage: POP3Mail <server[:port]> <username> <password|-|*|VARNAME> [TLS [true=implicit]]");
40              System.exit(1);
41          }
42  
43          final String[] arg0 = args[0].split(":");
44          final String server = arg0[0];
45          final String username = args[1];
46          String password = args[2];
47          // prompt for the password if necessary
48          try {
49              password = Utils.getPassword(username, password);
50          } catch (final IOException e1) {
51              System.err.println("Could not retrieve password: " + e1.getMessage());
52              return;
53          }
54  
55          final String proto = args.length > 3 ? args[3] : null;
56          final boolean implicit = args.length > 4 && Boolean.parseBoolean(args[4]);
57  
58          final POP3Client pop3;
59  
60          if (proto != null) {
61              System.out.println("Using secure protocol: " + proto);
62              pop3 = new POP3SClient(proto, implicit);
63          } else {
64              pop3 = new POP3Client();
65          }
66  
67          final int port;
68          if (arg0.length == 2) {
69              port = Integer.parseInt(arg0[1]);
70          } else {
71              port = pop3.getDefaultPort();
72          }
73          System.out.println("Connecting to server " + server + " on " + port);
74  
75          // We want to timeout if a response takes longer than 60 seconds
76          pop3.setDefaultTimeout(60000);
77  
78          // suppress login details
79          pop3.addProtocolCommandListener(PrintCommandListeners.sysOutPrintCommandListener());
80  
81          try {
82              pop3.connect(server);
83          } catch (final IOException e) {
84              System.err.println("Could not connect to server.");
85              e.printStackTrace();
86              return;
87          }
88  
89          try {
90              if (!pop3.login(username, password)) {
91                  System.err.println("Could not login to server.  Check password.");
92                  pop3.disconnect();
93                  return;
94              }
95  
96              final POP3MessageInfo status = pop3.status();
97              if (status == null) {
98                  System.err.println("Could not retrieve status.");
99                  pop3.logout();
100                 pop3.disconnect();
101                 return;
102             }
103 
104             System.out.println("Status: " + status);
105 
106             final POP3MessageInfo[] messages = pop3.listMessages();
107 
108             if (messages == null) {
109                 System.err.println("Could not retrieve message list.");
110                 pop3.logout();
111                 pop3.disconnect();
112                 return;
113             }
114             if (messages.length == 0) {
115                 System.out.println("No messages");
116                 pop3.logout();
117                 pop3.disconnect();
118                 return;
119             }
120 
121             System.out.println("Message count: " + messages.length);
122 
123             for (final POP3MessageInfo msginfo : messages) {
124                 final BufferedReader reader = (BufferedReader) pop3.retrieveMessageTop(msginfo.number, 0);
125 
126                 if (reader == null) {
127                     System.err.println("Could not retrieve message header.");
128                     pop3.logout();
129                     pop3.disconnect();
130                     return;
131                 }
132                 printMessageInfo(reader, msginfo.number);
133             }
134 
135             pop3.logout();
136             pop3.disconnect();
137         } catch (final IOException e) {
138             e.printStackTrace();
139         }
140     }
141 
142     public static void printMessageInfo(final BufferedReader reader, final int id) throws IOException {
143         String from = "";
144         String subject = "";
145         String line;
146         while ((line = reader.readLine()) != null) {
147             final String lower = line.toLowerCase(Locale.ENGLISH);
148             if (lower.startsWith("from: ")) {
149                 from = line.substring(6).trim();
150             } else if (lower.startsWith("subject: ")) {
151                 subject = line.substring(9).trim();
152             }
153         }
154 
155         System.out.println(Integer.toString(id) + " From: " + from + "  Subject: " + subject);
156     }
157 }