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