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