001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package examples.mail;
019
020import java.io.BufferedReader;
021import java.io.IOException;
022import java.io.PrintWriter;
023import java.util.Locale;
024
025import org.apache.commons.net.PrintCommandListener;
026import org.apache.commons.net.pop3.POP3Client;
027import org.apache.commons.net.pop3.POP3MessageInfo;
028import org.apache.commons.net.pop3.POP3SClient;
029
030/**
031 * This is an example program demonstrating how to use the POP3[S]Client class.
032 * This program connects to a POP3[S] server and retrieves the message
033 * headers of all the messages, printing the From: and Subject: header
034 * entries for each message.
035 * <p>
036 * See main() method for usage details
037 */
038public final class POP3Mail
039{
040
041    public static final void printMessageInfo(BufferedReader reader, int id) throws IOException  {
042        String from = "";
043        String subject = "";
044        String line;
045        while ((line = reader.readLine()) != null)
046        {
047            String lower = line.toLowerCase(Locale.ENGLISH);
048            if (lower.startsWith("from: ")) {
049                from = line.substring(6).trim();
050            }  else if (lower.startsWith("subject: ")) {
051                subject = line.substring(9).trim();
052            }
053        }
054
055        System.out.println(Integer.toString(id) + " From: " + from + "  Subject: " + subject);
056    }
057
058    public static void main(String[] args)
059    {
060        if (args.length < 3)
061        {
062            System.err.println(
063                "Usage: POP3Mail <server[:port]> <username> <password|-|*|VARNAME> [TLS [true=implicit]]");
064            System.exit(1);
065        }
066
067        String arg0[] = args[0].split(":");
068        String server=arg0[0];
069        String username = args[1];
070        String password = args[2];
071        // prompt for the password if necessary
072        try {
073            password = Utils.getPassword(username, password);
074        } catch (IOException e1) {
075            System.err.println("Could not retrieve password: " + e1.getMessage());
076            return;
077        }
078
079        String proto = args.length > 3 ? args[3] : null;
080        boolean implicit = args.length > 4 ? Boolean.parseBoolean(args[4]) : false;
081
082        POP3Client pop3;
083
084        if (proto != null) {
085            System.out.println("Using secure protocol: "+proto);
086            pop3 = new POP3SClient(proto, implicit);
087        } else {
088            pop3 = new POP3Client();
089        }
090
091        int port;
092        if (arg0.length == 2) {
093            port = Integer.parseInt(arg0[1]);
094        } else {
095            port = pop3.getDefaultPort();
096        }
097        System.out.println("Connecting to server "+server+" on "+port);
098
099        // We want to timeout if a response takes longer than 60 seconds
100        pop3.setDefaultTimeout(60000);
101
102        // suppress login details
103        pop3.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true));
104
105        try
106        {
107            pop3.connect(server);
108        }
109        catch (IOException e)
110        {
111            System.err.println("Could not connect to server.");
112            e.printStackTrace();
113            return;
114        }
115
116        try
117        {
118            if (!pop3.login(username, password))
119            {
120                System.err.println("Could not login to server.  Check password.");
121                pop3.disconnect();
122                return;
123            }
124
125            POP3MessageInfo status = pop3.status();
126            if (status == null) {
127                System.err.println("Could not retrieve status.");
128                pop3.logout();
129                pop3.disconnect();
130                return;
131            }
132
133            System.out.println("Status: " + status);
134
135            POP3MessageInfo[] messages = pop3.listMessages();
136
137            if (messages == null)
138            {
139                System.err.println("Could not retrieve message list.");
140                pop3.logout();
141                pop3.disconnect();
142                return;
143            }
144            else if (messages.length == 0)
145            {
146                System.out.println("No messages");
147                pop3.logout();
148                pop3.disconnect();
149                return;
150            }
151
152            System.out.println("Message count: " + messages.length);
153
154            for (POP3MessageInfo msginfo : messages) {
155                BufferedReader reader = (BufferedReader) pop3.retrieveMessageTop(msginfo.number, 0);
156
157                if (reader == null) {
158                    System.err.println("Could not retrieve message header.");
159                    pop3.logout();
160                    pop3.disconnect();
161                    return;
162                }
163                printMessageInfo(reader, msginfo.number);
164            }
165
166            pop3.logout();
167            pop3.disconnect();
168        }
169        catch (IOException e)
170        {
171            e.printStackTrace();
172            return;
173        }
174    }
175}
176