You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by se...@apache.org on 2017/03/24 20:00:09 UTC
svn commit: r1788535 - in /commons/proper/net/trunk/src: changes/changes.xml
main/java/examples/mail/POP3ExportMbox.java
main/resources/examples/examples.properties
Author: sebb
Date: Fri Mar 24 20:00:09 2017
New Revision: 1788535
URL: http://svn.apache.org/viewvc?rev=1788535&view=rev
Log:
Add POP3ExportMbox example code
Added:
commons/proper/net/trunk/src/main/java/examples/mail/POP3ExportMbox.java (with props)
Modified:
commons/proper/net/trunk/src/changes/changes.xml
commons/proper/net/trunk/src/main/resources/examples/examples.properties
Modified: commons/proper/net/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/net/trunk/src/changes/changes.xml?rev=1788535&r1=1788534&r2=1788535&view=diff
==============================================================================
--- commons/proper/net/trunk/src/changes/changes.xml [utf-8] (original)
+++ commons/proper/net/trunk/src/changes/changes.xml [utf-8] Fri Mar 24 20:00:09 2017
@@ -71,6 +71,9 @@ This is mainly a bug-fix release. See fu
However it is not source compatible with releases before 3.4, as some methods were added to the interface NtpV3Packet in 3.4
">
+ <action type="add" dev="sebb">
+ Add POP3ExportMbox example code
+ </action>
<action issue="NET-633" type="update" dev="sebb" due-to="n0rm1e">
Add XOAUTH2 to IMAP and SMTP
</action>
Added: commons/proper/net/trunk/src/main/java/examples/mail/POP3ExportMbox.java
URL: http://svn.apache.org/viewvc/commons/proper/net/trunk/src/main/java/examples/mail/POP3ExportMbox.java?rev=1788535&view=auto
==============================================================================
--- commons/proper/net/trunk/src/main/java/examples/mail/POP3ExportMbox.java (added)
+++ commons/proper/net/trunk/src/main/java/examples/mail/POP3ExportMbox.java Fri Mar 24 20:00:09 2017
@@ -0,0 +1,188 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package examples.mail;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.nio.charset.Charset;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.apache.commons.net.pop3.POP3Client;
+import org.apache.commons.net.pop3.POP3MessageInfo;
+import org.apache.commons.net.pop3.POP3SClient;
+
+/**
+ * This is an example program demonstrating how to use the POP3[S]Client class.
+ * This program connects to a POP3[S] server and writes the messages
+ * to an mbox file.
+ * <p>
+ * The code currently assumes that POP3Client decodes the POP3 data as iso-8859-1.
+ * The POP3 standard only allows for ASCII so in theory iso-8859-1 should be OK.
+ * However it appears that actual POP3 implementations may return 8bit data that is
+ * outside the ASCII range; this may result in loss of data when the mailbox is created.
+ * <p>
+ * See main() method for usage details
+ */
+public final class POP3ExportMbox
+{
+
+ private static final Pattern PATFROM = Pattern.compile(">*From "); // unescaped From_
+
+ public static void main(String[] args)
+ {
+ int argIdx;
+ String file = null;
+ for(argIdx = 0; argIdx < args.length; argIdx++) {
+ if (args[argIdx].equals("-F")) {
+ file = args[++argIdx];
+ } else {
+ break;
+ }
+ }
+
+ final int argCount = args.length - argIdx;
+ if (argCount < 3)
+ {
+ System.err.println(
+ "Usage: POP3Mail [-F file] <server[:port]> <username> <password|-|*|VARNAME> [TLS [true=implicit]]");
+ System.exit(1);
+ }
+
+ String arg0[] = args[argIdx++].split(":");
+ String server=arg0[0];
+ String username = args[argIdx++];
+ String password = args[argIdx++];
+ // prompt for the password if necessary
+ try {
+ password = Utils.getPassword(username, password);
+ } catch (IOException e1) {
+ System.err.println("Could not retrieve password: " + e1.getMessage());
+ return;
+ }
+
+ String proto = argCount > 3 ? args[argIdx++] : null;
+ boolean implicit = argCount > 4 ? Boolean.parseBoolean(args[argIdx++]) : false;
+
+ POP3Client pop3;
+
+ if (proto != null) {
+ System.out.println("Using secure protocol: "+proto);
+ pop3 = new POP3SClient(proto, implicit);
+ } else {
+ pop3 = new POP3Client();
+ }
+
+ int port;
+ if (arg0.length == 2) {
+ port = Integer.parseInt(arg0[1]);
+ } else {
+ port = pop3.getDefaultPort();
+ }
+ System.out.println("Connecting to server "+server+" on "+port);
+
+ // We want to timeout if a response takes longer than 60 seconds
+ pop3.setDefaultTimeout(60000);
+
+ try
+ {
+ pop3.connect(server);
+ }
+ catch (IOException e)
+ {
+ System.err.println("Could not connect to server.");
+ e.printStackTrace();
+ return;
+ }
+
+ try
+ {
+ if (!pop3.login(username, password))
+ {
+ System.err.println("Could not login to server. Check password.");
+ pop3.disconnect();
+ return;
+ }
+
+ POP3MessageInfo status = pop3.status();
+ if (status == null) {
+ System.err.println("Could not retrieve status.");
+ pop3.logout();
+ pop3.disconnect();
+ return;
+ }
+
+ System.out.println("Status: " + status);
+ int count = status.number;
+ 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);
+ }
+ fw.close();
+ }
+
+ pop3.logout();
+ pop3.disconnect();
+ }
+ catch (IOException e)
+ {
+ e.printStackTrace();
+ return;
+ }
+ }
+
+ 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");
+ String replyTo = "MAILER-DAEMON"; // default
+ Date received = new Date();
+ BufferedReader r = (BufferedReader) pop3.retrieveMessage(i);
+ fw.append("From ");
+ fw.append(replyTo);
+ fw.append(' ');
+ fw.append(DATE_FORMAT.format(received));
+ fw.append("\n");
+ String line;
+ while ((line = r.readLine()) != null)
+ {
+ if (startsWith(line, PATFROM)) {
+ fw.write(">");
+ }
+ fw.write(line);
+ fw.write("\n");
+ }
+ fw.write("\n");
+ r.close();
+ }
+
+ private static boolean startsWith(String input, Pattern pat) {
+ Matcher m = pat.matcher(input);
+ return m.lookingAt();
+ }
+}
+
Propchange: commons/proper/net/trunk/src/main/java/examples/mail/POP3ExportMbox.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: commons/proper/net/trunk/src/main/resources/examples/examples.properties
URL: http://svn.apache.org/viewvc/commons/proper/net/trunk/src/main/resources/examples/examples.properties?rev=1788535&r1=1788534&r2=1788535&view=diff
==============================================================================
--- commons/proper/net/trunk/src/main/resources/examples/examples.properties (original)
+++ commons/proper/net/trunk/src/main/resources/examples/examples.properties Fri Mar 24 20:00:09 2017
@@ -28,6 +28,7 @@ TFTPExample examples/ftp/T
IMAPExportMbox examples/mail/IMAPExportMbox
IMAPImportMbox examples/mail/IMAPImportMbox
IMAPMail examples/mail/IMAPMail
+POP3ExportMbox examples/mail/POP3ExportMbox
POP3Mail examples/mail/POP3Mail
SMTPMail examples/mail/SMTPMail
ArticleReader examples/nntp/ArticleReader