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