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   * <p>
38   */
39  public final class POP3Mail
40  {
41  
42      public static final void printMessageInfo(BufferedReader reader, int id) throws IOException  {
43          String from = "";
44          String subject = "";
45          String line;
46          while ((line = reader.readLine()) != null)
47          {
48              String lower = line.toLowerCase(Locale.ENGLISH);
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  
56          System.out.println(Integer.toString(id) + " From: " + from + "  Subject: " + subject);
57      }
58  
59      public static void main(String[] args)
60      {
61          if (args.length < 3)
62          {
63              System.err.println(
64                  "Usage: POP3Mail <pop3 server hostname> <username> <password> [TLS [true=implicit]]");
65              System.exit(1);
66          }
67  
68          String server = args[0];
69          String username = args[1];
70          String password = args[2];
71  
72          String proto = args.length > 3 ? args[3] : null;
73          boolean implicit = args.length > 4 ? Boolean.parseBoolean(args[4]) : false;
74  
75          POP3Client pop3;
76  
77          if (proto != null) {
78              System.out.println("Using secure protocol: "+proto);
79              pop3 = new POP3SClient(proto, implicit);
80          } else {
81              pop3 = new POP3Client();
82          }
83          System.out.println("Connecting to server "+server+" on "+pop3.getDefaultPort());
84  
85          // We want to timeout if a response takes longer than 60 seconds
86          pop3.setDefaultTimeout(60000);
87  
88          // suppress login details
89          pop3.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true));
90  
91          try
92          {
93              pop3.connect(server);
94          }
95          catch (IOException e)
96          {
97              System.err.println("Could not connect to server.");
98              e.printStackTrace();
99              System.exit(1);
100         }
101 
102         try
103         {
104             if (!pop3.login(username, password))
105             {
106                 System.err.println("Could not login to server.  Check password.");
107                 pop3.disconnect();
108                 System.exit(1);
109             }
110 
111             POP3MessageInfo[] messages = pop3.listMessages();
112 
113             if (messages == null)
114             {
115                 System.err.println("Could not retrieve message list.");
116                 pop3.disconnect();
117                 return;
118             }
119             else if (messages.length == 0)
120             {
121                 System.out.println("No messages");
122                 pop3.logout();
123                 pop3.disconnect();
124                 return;
125             }
126 
127             for (POP3MessageInfo msginfo : messages) {
128                 BufferedReader reader = (BufferedReader) pop3.retrieveMessageTop(msginfo.number, 0);
129 
130                 if (reader == null) {
131                     System.err.println("Could not retrieve message header.");
132                     pop3.disconnect();
133                     System.exit(1);
134                 }
135                 printMessageInfo(reader, msginfo.number);
136             }
137 
138             pop3.logout();
139             pop3.disconnect();
140         }
141         catch (IOException e)
142         {
143             e.printStackTrace();
144             return;
145         }
146     }
147 }
148