1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
32
33
34
35
36
37
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
86 pop3.setDefaultTimeout(60000);
87
88
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