001    /*
002     * Copyright 2001-2005 The Apache Software Foundation
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *     http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package examples;
017    
018    import java.io.BufferedReader;
019    import java.io.IOException;
020    import java.io.Reader;
021    import org.apache.commons.net.pop3.POP3Client;
022    import org.apache.commons.net.pop3.POP3MessageInfo;
023    
024    /***
025     * This is an example program demonstrating how to use the POP3Client class.
026     * This program connects to a POP3 server and retrieves the message
027     * headers of all the messages, printing the From: and Subject: header
028     * entries for each message.
029     * <p>
030     * Usage: messages <pop3 server hostname> <username> <password>
031     * <p>
032     ***/
033    public final class messages
034    {
035    
036        public static final void printMessageInfo(BufferedReader reader, int id)
037        throws IOException
038        {
039            String line, lower, from, subject;
040    
041            from = "";
042            subject = "";
043    
044            while ((line = reader.readLine()) != null)
045            {
046                lower = line.toLowerCase();
047                if (lower.startsWith("from: "))
048                    from = line.substring(6).trim();
049                else if (lower.startsWith("subject: "))
050                    subject = line.substring(9).trim();
051            }
052    
053            System.out.println(Integer.toString(id) + " From: " + from +
054                               "  Subject: " + subject);
055        }
056    
057        public static final void main(String[] args)
058        {
059            int message;
060            String server, username, password;
061            POP3Client pop3;
062            Reader reader;
063            POP3MessageInfo[] messages;
064    
065            if (args.length < 3)
066            {
067                System.err.println(
068                    "Usage: messages <pop3 server hostname> <username> <password>");
069                System.exit(1);
070            }
071    
072            server = args[0];
073            username = args[1];
074            password = args[2];
075    
076            pop3 = new POP3Client();
077            // We want to timeout if a response takes longer than 60 seconds
078            pop3.setDefaultTimeout(60000);
079    
080            try
081            {
082                pop3.connect(server);
083            }
084            catch (IOException e)
085            {
086                System.err.println("Could not connect to server.");
087                e.printStackTrace();
088                System.exit(1);
089            }
090    
091            try
092            {
093                if (!pop3.login(username, password))
094                {
095                    System.err.println("Could not login to server.  Check password.");
096                    pop3.disconnect();
097                    System.exit(1);
098                }
099    
100                messages = pop3.listMessages();
101    
102                if (messages == null)
103                {
104                    System.err.println("Could not retrieve message list.");
105                    pop3.disconnect();
106                    System.exit(1);
107                }
108                else if (messages.length == 0)
109                {
110                    System.out.println("No messages");
111                    pop3.logout();
112                    pop3.disconnect();
113                    System.exit(1);
114                }
115    
116                for (message = 0; message < messages.length; message++)
117                {
118                    reader = pop3.retrieveMessageTop(messages[message].number, 0);
119    
120                    if (reader == null)
121                    {
122                        System.err.println("Could not retrieve message header.");
123                        pop3.disconnect();
124                        System.exit(1);
125                    }
126    
127                    printMessageInfo(new BufferedReader(reader), messages[message].number);
128                }
129    
130                pop3.logout();
131                pop3.disconnect();
132            }
133            catch (IOException e)
134            {
135                e.printStackTrace();
136                System.exit(1);
137            }
138        }
139    }
140