1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
32
33
34
35
36 public final class POP3Mail {
37
38 public static void main(final String[] args) {
39 if (args.length < 3) {
40 System.err.println("Usage: POP3Mail <server[:port]> <username> <password|-|*|VARNAME> [TLS [true=implicit]]");
41 System.exit(1);
42 }
43
44 final String[] arg0 = args[0].split(":");
45 final String server = arg0[0];
46 final String username = args[1];
47 String password = args[2];
48
49 try {
50 password = Utils.getPassword(username, password);
51 } catch (final IOException e1) {
52 System.err.println("Could not retrieve password: " + e1.getMessage());
53 return;
54 }
55
56 final String proto = args.length > 3 ? args[3] : null;
57 final boolean implicit = args.length > 4 && Boolean.parseBoolean(args[4]);
58
59 final POP3Client pop3;
60
61 if (proto != null) {
62 System.out.println("Using secure protocol: " + proto);
63 pop3 = new POP3SClient(proto, implicit);
64 } else {
65 pop3 = new POP3Client();
66 }
67
68 final int port;
69 if (arg0.length == 2) {
70 port = Integer.parseInt(arg0[1]);
71 } else {
72 port = pop3.getDefaultPort();
73 }
74 System.out.println("Connecting to server " + server + " on " + port);
75
76
77 pop3.setDefaultTimeout(60000);
78
79
80 pop3.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true));
81
82 try {
83 pop3.connect(server);
84 } catch (final IOException e) {
85 System.err.println("Could not connect to server.");
86 e.printStackTrace();
87 return;
88 }
89
90 try {
91 if (!pop3.login(username, password)) {
92 System.err.println("Could not login to server. Check password.");
93 pop3.disconnect();
94 return;
95 }
96
97 final POP3MessageInfo status = pop3.status();
98 if (status == null) {
99 System.err.println("Could not retrieve status.");
100 pop3.logout();
101 pop3.disconnect();
102 return;
103 }
104
105 System.out.println("Status: " + status);
106
107 final POP3MessageInfo[] messages = pop3.listMessages();
108
109 if (messages == null) {
110 System.err.println("Could not retrieve message list.");
111 pop3.logout();
112 pop3.disconnect();
113 return;
114 }
115 if (messages.length == 0) {
116 System.out.println("No messages");
117 pop3.logout();
118 pop3.disconnect();
119 return;
120 }
121
122 System.out.println("Message count: " + messages.length);
123
124 for (final POP3MessageInfo msginfo : messages) {
125 final BufferedReader reader = (BufferedReader) pop3.retrieveMessageTop(msginfo.number, 0);
126
127 if (reader == null) {
128 System.err.println("Could not retrieve message header.");
129 pop3.logout();
130 pop3.disconnect();
131 return;
132 }
133 printMessageInfo(reader, msginfo.number);
134 }
135
136 pop3.logout();
137 pop3.disconnect();
138 } catch (final IOException e) {
139 e.printStackTrace();
140 }
141 }
142
143 public static void printMessageInfo(final BufferedReader reader, final int id) throws IOException {
144 String from = "";
145 String subject = "";
146 String line;
147 while ((line = reader.readLine()) != null) {
148 final String lower = line.toLowerCase(Locale.ENGLISH);
149 if (lower.startsWith("from: ")) {
150 from = line.substring(6).trim();
151 } else if (lower.startsWith("subject: ")) {
152 subject = line.substring(9).trim();
153 }
154 }
155
156 System.out.println(Integer.toString(id) + " From: " + from + " Subject: " + subject);
157 }
158 }