commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From s...@apache.org
Subject svn commit: r1792689 - /commons/proper/net/trunk/src/main/java/org/apache/commons/net/examples/mail/POP3ExportMbox.java
Date Wed, 26 Apr 2017 00:49:15 GMT
Author: sebb
Date: Wed Apr 26 00:49:15 2017
New Revision: 1792689

URL: http://svn.apache.org/viewvc?rev=1792689&view=rev
Log:
Write individual .eml files if output is a directory

Modified:
    commons/proper/net/trunk/src/main/java/org/apache/commons/net/examples/mail/POP3ExportMbox.java

Modified: commons/proper/net/trunk/src/main/java/org/apache/commons/net/examples/mail/POP3ExportMbox.java
URL: http://svn.apache.org/viewvc/commons/proper/net/trunk/src/main/java/org/apache/commons/net/examples/mail/POP3ExportMbox.java?rev=1792689&r1=1792688&r2=1792689&view=diff
==============================================================================
--- commons/proper/net/trunk/src/main/java/org/apache/commons/net/examples/mail/POP3ExportMbox.java
(original)
+++ commons/proper/net/trunk/src/main/java/org/apache/commons/net/examples/mail/POP3ExportMbox.java
Wed Apr 26 00:49:15 2017
@@ -65,7 +65,7 @@ public final class POP3ExportMbox
         if (argCount < 3)
         {
             System.err.println(
-                "Usage: POP3Mail [-F file] <server[:port]> <username> <password|-|*|VARNAME>
[TLS [true=implicit]]");
+                "Usage: POP3Mail [-F file/directory] <server[:port]> <username>
<password|-|*|VARNAME> [TLS [true=implicit]]");
             System.exit(1);
         }
 
@@ -137,13 +137,23 @@ public final class POP3ExportMbox
             if (file != null) {
                 System.out.println("Getting messages: " + count);
                 File mbox = new File(file);
-                System.out.println("Writing: " + mbox);
-                // Currently POP3Client uses iso-8859-1
-                OutputStreamWriter fw = new OutputStreamWriter(new FileOutputStream(mbox),Charset.forName("iso-8859-1"));
-                for (int i = 1; i <= count; i++) {
-                    writeMbox(pop3, fw, i);
+                if (mbox.isDirectory()) {
+                    System.out.println("Writing dir: " + mbox);
+                    // Currently POP3Client uses iso-8859-1
+                    for (int i = 1; i <= count; i++) {
+                        OutputStreamWriter fw = new OutputStreamWriter(new FileOutputStream(new
File(mbox,i+".eml")),Charset.forName("iso-8859-1"));
+                        writeFile(pop3, fw, i);
+                        fw.close();                    
+                    }
+                } else {
+                    System.out.println("Writing file: " + mbox);
+                    // Currently POP3Client uses iso-8859-1
+                    OutputStreamWriter fw = new OutputStreamWriter(new FileOutputStream(mbox),Charset.forName("iso-8859-1"));
+                    for (int i = 1; i <= count; i++) {
+                        writeMbox(pop3, fw, i);
+                    }
+                    fw.close();                    
                 }
-                fw.close();
             }
 
             pop3.logout();
@@ -156,6 +166,17 @@ public final class POP3ExportMbox
         }
     }
 
+    private static void writeFile(POP3Client pop3, OutputStreamWriter fw, int i) throws IOException
{
+        BufferedReader r = (BufferedReader) pop3.retrieveMessage(i);
+        String line;
+        while ((line = r.readLine()) != null)
+        {
+            fw.write(line);
+            fw.write("\n");
+        }
+        r.close();
+    }
+
     private static void writeMbox(POP3Client pop3, OutputStreamWriter fw, int i) throws IOException
{
         final SimpleDateFormat DATE_FORMAT // for mbox From_ lines
         = new SimpleDateFormat("EEE MMM dd HH:mm:ss YYYY");



Mime
View raw message