Skip to content

Commit 1863723

Browse files
committed
Write individual .eml files if output is a directory
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/net/trunk@1792689 13f79535-47bb-0310-9956-ffa450edef68
1 parent 7fabd00 commit 1863723

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed

src/main/java/org/apache/commons/net/examples/mail/POP3ExportMbox.java

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public static void main(String[] args)
6565
if (argCount < 3)
6666
{
6767
System.err.println(
68-
"Usage: POP3Mail [-F file] <server[:port]> <username> <password|-|*|VARNAME> [TLS [true=implicit]]");
68+
"Usage: POP3Mail [-F file/directory] <server[:port]> <username> <password|-|*|VARNAME> [TLS [true=implicit]]");
6969
System.exit(1);
7070
}
7171

@@ -137,13 +137,23 @@ public static void main(String[] args)
137137
if (file != null) {
138138
System.out.println("Getting messages: " + count);
139139
File mbox = new File(file);
140-
System.out.println("Writing: " + mbox);
141-
// Currently POP3Client uses iso-8859-1
142-
OutputStreamWriter fw = new OutputStreamWriter(new FileOutputStream(mbox),Charset.forName("iso-8859-1"));
143-
for (int i = 1; i <= count; i++) {
144-
writeMbox(pop3, fw, i);
140+
if (mbox.isDirectory()) {
141+
System.out.println("Writing dir: " + mbox);
142+
// Currently POP3Client uses iso-8859-1
143+
for (int i = 1; i <= count; i++) {
144+
OutputStreamWriter fw = new OutputStreamWriter(new FileOutputStream(new File(mbox,i+".eml")),Charset.forName("iso-8859-1"));
145+
writeFile(pop3, fw, i);
146+
fw.close();
147+
}
148+
} else {
149+
System.out.println("Writing file: " + mbox);
150+
// Currently POP3Client uses iso-8859-1
151+
OutputStreamWriter fw = new OutputStreamWriter(new FileOutputStream(mbox),Charset.forName("iso-8859-1"));
152+
for (int i = 1; i <= count; i++) {
153+
writeMbox(pop3, fw, i);
154+
}
155+
fw.close();
145156
}
146-
fw.close();
147157
}
148158

149159
pop3.logout();
@@ -156,6 +166,17 @@ public static void main(String[] args)
156166
}
157167
}
158168

169+
private static void writeFile(POP3Client pop3, OutputStreamWriter fw, int i) throws IOException {
170+
BufferedReader r = (BufferedReader) pop3.retrieveMessage(i);
171+
String line;
172+
while ((line = r.readLine()) != null)
173+
{
174+
fw.write(line);
175+
fw.write("\n");
176+
}
177+
r.close();
178+
}
179+
159180
private static void writeMbox(POP3Client pop3, OutputStreamWriter fw, int i) throws IOException {
160181
final SimpleDateFormat DATE_FORMAT // for mbox From_ lines
161182
= new SimpleDateFormat("EEE MMM dd HH:mm:ss YYYY");

0 commit comments

Comments
 (0)