POP3Mail.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.apache.commons.net.examples.mail;

  18. import java.io.BufferedReader;
  19. import java.io.IOException;
  20. import java.util.Locale;

  21. import org.apache.commons.net.PrintCommandListener;
  22. import org.apache.commons.net.io.Util;
  23. import org.apache.commons.net.pop3.POP3Client;
  24. import org.apache.commons.net.pop3.POP3MessageInfo;
  25. import org.apache.commons.net.pop3.POP3SClient;

  26. /**
  27.  * This is an example program demonstrating how to use the POP3[S]Client class. This program connects to a POP3[S] server and retrieves the message headers of
  28.  * all the messages, printing the {@code From:} and {@code Subject:} header entries for each message.
  29.  * <p>
  30.  * See main() method for usage details
  31.  */
  32. public final class POP3Mail {

  33.     public static void main(final String[] args) {
  34.         if (args.length < 3) {
  35.             System.err.println("Usage: POP3Mail <server[:port]> <username> <password|-|*|VARNAME> [TLS [true=implicit]]");
  36.             System.exit(1);
  37.         }

  38.         final String[] arg0 = args[0].split(":");
  39.         final String server = arg0[0];
  40.         final String username = args[1];
  41.         String password = args[2];
  42.         // prompt for the password if necessary
  43.         try {
  44.             password = Utils.getPassword(username, password);
  45.         } catch (final IOException e1) {
  46.             System.err.println("Could not retrieve password: " + e1.getMessage());
  47.             return;
  48.         }

  49.         final String proto = args.length > 3 ? args[3] : null;
  50.         final boolean implicit = args.length > 4 && Boolean.parseBoolean(args[4]);

  51.         final POP3Client pop3;

  52.         if (proto != null) {
  53.             System.out.println("Using secure protocol: " + proto);
  54.             pop3 = new POP3SClient(proto, implicit);
  55.         } else {
  56.             pop3 = new POP3Client();
  57.         }

  58.         final int port;
  59.         if (arg0.length == 2) {
  60.             port = Integer.parseInt(arg0[1]);
  61.         } else {
  62.             port = pop3.getDefaultPort();
  63.         }
  64.         System.out.println("Connecting to server " + server + " on " + port);

  65.         // We want to timeout if a response takes longer than 60 seconds
  66.         pop3.setDefaultTimeout(60000);

  67.         // suppress login details
  68.         pop3.addProtocolCommandListener(new PrintCommandListener(Util.newPrintWriter(System.out), true));

  69.         try {
  70.             pop3.connect(server);
  71.         } catch (final IOException e) {
  72.             System.err.println("Could not connect to server.");
  73.             e.printStackTrace();
  74.             return;
  75.         }

  76.         try {
  77.             if (!pop3.login(username, password)) {
  78.                 System.err.println("Could not login to server.  Check password.");
  79.                 pop3.disconnect();
  80.                 return;
  81.             }

  82.             final POP3MessageInfo status = pop3.status();
  83.             if (status == null) {
  84.                 System.err.println("Could not retrieve status.");
  85.                 pop3.logout();
  86.                 pop3.disconnect();
  87.                 return;
  88.             }

  89.             System.out.println("Status: " + status);

  90.             final POP3MessageInfo[] messages = pop3.listMessages();

  91.             if (messages == null) {
  92.                 System.err.println("Could not retrieve message list.");
  93.                 pop3.logout();
  94.                 pop3.disconnect();
  95.                 return;
  96.             }
  97.             if (messages.length == 0) {
  98.                 System.out.println("No messages");
  99.                 pop3.logout();
  100.                 pop3.disconnect();
  101.                 return;
  102.             }

  103.             System.out.println("Message count: " + messages.length);

  104.             for (final POP3MessageInfo msginfo : messages) {
  105.                 final BufferedReader reader = (BufferedReader) pop3.retrieveMessageTop(msginfo.number, 0);

  106.                 if (reader == null) {
  107.                     System.err.println("Could not retrieve message header.");
  108.                     pop3.logout();
  109.                     pop3.disconnect();
  110.                     return;
  111.                 }
  112.                 printMessageInfo(reader, msginfo.number);
  113.             }

  114.             pop3.logout();
  115.             pop3.disconnect();
  116.         } catch (final IOException e) {
  117.             e.printStackTrace();
  118.         }
  119.     }

  120.     public static void printMessageInfo(final BufferedReader reader, final int id) throws IOException {
  121.         String from = "";
  122.         String subject = "";
  123.         String line;
  124.         while ((line = reader.readLine()) != null) {
  125.             final String lower = line.toLowerCase(Locale.ENGLISH);
  126.             if (lower.startsWith("from: ")) {
  127.                 from = line.substring(6).trim();
  128.             } else if (lower.startsWith("subject: ")) {
  129.                 subject = line.substring(9).trim();
  130.             }
  131.         }

  132.         System.out.println(Integer.toString(id) + " From: " + from + "  Subject: " + subject);
  133.     }
  134. }