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