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 org.apache.commons.net.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  * See main() method for usage details
37  */
38 public final class POP3Mail
39 {
40 
printMessageInfo(final BufferedReader reader, final int id)41     public static void printMessageInfo(final BufferedReader reader, final int id) throws IOException  {
42         String from = "";
43         String subject = "";
44         String line;
45         while ((line = reader.readLine()) != null)
46         {
47             final 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 
main(final String[] args)58     public static void main(final String[] args)
59     {
60         if (args.length < 3)
61         {
62             System.err.println(
63                 "Usage: POP3Mail <server[:port]> <username> <password|-|*|VARNAME> [TLS [true=implicit]]");
64             System.exit(1);
65         }
66 
67         final String arg0[] = args[0].split(":");
68         final String server=arg0[0];
69         final String username = args[1];
70         String password = args[2];
71         // prompt for the password if necessary
72         try {
73             password = Utils.getPassword(username, password);
74         } catch (final IOException e1) {
75             System.err.println("Could not retrieve password: " + e1.getMessage());
76             return;
77         }
78 
79         final String proto = args.length > 3 ? args[3] : null;
80         final boolean implicit = args.length > 4 ? Boolean.parseBoolean(args[4]) : false;
81 
82         final POP3Client pop3;
83 
84         if (proto != null) {
85             System.out.println("Using secure protocol: "+proto);
86             pop3 = new POP3SClient(proto, implicit);
87         } else {
88             pop3 = new POP3Client();
89         }
90 
91         final int port;
92         if (arg0.length == 2) {
93             port = Integer.parseInt(arg0[1]);
94         } else {
95             port = pop3.getDefaultPort();
96         }
97         System.out.println("Connecting to server "+server+" on "+port);
98 
99         // We want to timeout if a response takes longer than 60 seconds
100         pop3.setDefaultTimeout(60000);
101 
102         // suppress login details
103         pop3.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true));
104 
105         try
106         {
107             pop3.connect(server);
108         }
109         catch (final IOException e)
110         {
111             System.err.println("Could not connect to server.");
112             e.printStackTrace();
113             return;
114         }
115 
116         try
117         {
118             if (!pop3.login(username, password))
119             {
120                 System.err.println("Could not login to server.  Check password.");
121                 pop3.disconnect();
122                 return;
123             }
124 
125             final POP3MessageInfo status = pop3.status();
126             if (status == null) {
127                 System.err.println("Could not retrieve status.");
128                 pop3.logout();
129                 pop3.disconnect();
130                 return;
131             }
132 
133             System.out.println("Status: " + status);
134 
135             final POP3MessageInfo[] messages = pop3.listMessages();
136 
137             if (messages == null)
138             {
139                 System.err.println("Could not retrieve message list.");
140                 pop3.logout();
141                 pop3.disconnect();
142                 return;
143             }
144             else if (messages.length == 0)
145             {
146                 System.out.println("No messages");
147                 pop3.logout();
148                 pop3.disconnect();
149                 return;
150             }
151 
152             System.out.println("Message count: " + messages.length);
153 
154             for (final POP3MessageInfo msginfo : messages) {
155                 final BufferedReader reader = (BufferedReader) pop3.retrieveMessageTop(msginfo.number, 0);
156 
157                 if (reader == null) {
158                     System.err.println("Could not retrieve message header.");
159                     pop3.logout();
160                     pop3.disconnect();
161                     return;
162                 }
163                 printMessageInfo(reader, msginfo.number);
164             }
165 
166             pop3.logout();
167             pop3.disconnect();
168         }
169         catch (final IOException e)
170         {
171             e.printStackTrace();
172             return;
173         }
174     }
175 }
176 
177