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    
018    package examples;
019    
020    import java.io.BufferedReader;
021    import java.io.IOException;
022    import java.io.Reader;
023    import org.apache.commons.net.pop3.POP3Client;
024    import org.apache.commons.net.pop3.POP3MessageInfo;
025    
026    /***
027     * This is an example program demonstrating how to use the POP3Client class.
028     * This program connects to a POP3 server and retrieves the message
029     * headers of all the messages, printing the From: and Subject: header
030     * entries for each message.
031     * <p>
032     * Usage: messages <pop3 server hostname> <username> <password>
033     * <p>
034     ***/
035    public final class messages
036    {
037    
038        public static final void printMessageInfo(BufferedReader reader, int id)
039        throws IOException
040        {
041            String line, lower, from, subject;
042    
043            from = "";
044            subject = "";
045    
046            while ((line = reader.readLine()) != null)
047            {
048                lower = line.toLowerCase();
049                if (lower.startsWith("from: "))
050                    from = line.substring(6).trim();
051                else if (lower.startsWith("subject: "))
052                    subject = line.substring(9).trim();
053            }
054    
055            System.out.println(Integer.toString(id) + " From: " + from +
056                               "  Subject: " + subject);
057        }
058    
059        public static final void main(String[] args)
060        {
061            int message;
062            String server, username, password;
063            POP3Client pop3;
064            Reader reader;
065            POP3MessageInfo[] messages;
066    
067            if (args.length < 3)
068            {
069                System.err.println(
070                    "Usage: messages <pop3 server hostname> <username> <password>");
071                System.exit(1);
072            }
073    
074            server = args[0];
075            username = args[1];
076            password = args[2];
077    
078            pop3 = new POP3Client();
079            // We want to timeout if a response takes longer than 60 seconds
080            pop3.setDefaultTimeout(60000);
081    
082            try
083            {
084                pop3.connect(server);
085            }
086            catch (IOException e)
087            {
088                System.err.println("Could not connect to server.");
089                e.printStackTrace();
090                System.exit(1);
091            }
092    
093            try
094            {
095                if (!pop3.login(username, password))
096                {
097                    System.err.println("Could not login to server.  Check password.");
098                    pop3.disconnect();
099                    System.exit(1);
100                }
101    
102                messages = pop3.listMessages();
103    
104                if (messages == null)
105                {
106                    System.err.println("Could not retrieve message list.");
107                    pop3.disconnect();
108                    System.exit(1);
109                }
110                else if (messages.length == 0)
111                {
112                    System.out.println("No messages");
113                    pop3.logout();
114                    pop3.disconnect();
115                    System.exit(1);
116                }
117    
118                for (message = 0; message < messages.length; message++)
119                {
120                    reader = pop3.retrieveMessageTop(messages[message].number, 0);
121    
122                    if (reader == null)
123                    {
124                        System.err.println("Could not retrieve message header.");
125                        pop3.disconnect();
126                        System.exit(1);
127                    }
128    
129                    printMessageInfo(new BufferedReader(reader), messages[message].number);
130                }
131    
132                pop3.logout();
133                pop3.disconnect();
134            }
135            catch (IOException e)
136            {
137                e.printStackTrace();
138                System.exit(1);
139            }
140        }
141    }
142