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.IOException;
021import java.net.URI;
022
023import org.apache.commons.net.PrintCommandListener;
024import org.apache.commons.net.imap.IMAPClient;
025
026/**
027 * This is an example program demonstrating how to use the IMAP[S]Client class.
028 * This program connects to a IMAP[S] server, lists its capabilities and shows
029 * the status of the Inbox.
030 * <p>
031 * Usage: IMAPMail imap[s]://username:password@server/
032 * <p>
033 * For example<br>
034 * IMAPMail imaps://username:password@imap.mail.yahoo.com/<br>
035 * or<br>
036 * IMAPMail imaps://username:password@imap.googlemail.com/
037 */
038public final class IMAPMail
039{
040
041    public static void main(String[] args) throws IOException {
042        if (args.length != 1)
043        {
044            System.err.println(
045                "Usage: IMAPMail imap[s]://username:password@server/");
046            System.err.println("Connects to server; lists capabilities and shows Inbox status");
047            System.exit(1);
048        }
049
050        URI uri = URI.create(args[0]);
051
052        // Connect and login
053        final IMAPClient imap = IMAPUtils.imapLogin(uri, 10000, null);
054
055        // suppress login details
056        imap.addProtocolCommandListener(new PrintCommandListener(System.out, true));
057
058        try {
059            imap.setSoTimeout(6000);
060
061            imap.capability();
062
063            imap.select("inbox");
064
065            imap.examine("inbox");
066
067            imap.status("inbox", new String[]{"MESSAGES"});
068
069        } catch (IOException e) {
070            System.out.println(imap.getReplyString());
071            e.printStackTrace();
072            System.exit(10);
073            return;
074        } finally {
075            imap.logout();
076            imap.disconnect();
077        }
078    }
079}
080
081/* kate: indent-width 4; replace-tabs on; */