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