Example usage for org.apache.commons.net.imap IMAPClient capability

List of usage examples for org.apache.commons.net.imap IMAPClient capability

Introduction

In this page you can find the example usage for org.apache.commons.net.imap IMAPClient capability.

Prototype

public boolean capability() throws IOException 

Source Link

Document

Send a CAPABILITY command to the server.

Usage

From source file:examples.mail.IMAPMail.java

public static void main(String[] args) {
    if (args.length < 3) {
        System.err.println("Usage: IMAPMail <imap server hostname> <username> <password> [TLS]");
        System.exit(1);/*from  w  w  w  .j  av  a 2 s  . co  m*/
    }

    String server = args[0];
    String username = args[1];
    String password = args[2];

    String proto = (args.length > 3) ? args[3] : null;

    IMAPClient imap;

    if (proto != null) {
        System.out.println("Using secure protocol: " + proto);
        imap = new IMAPSClient(proto, true); // implicit
        // enable the next line to only check if the server certificate has expired (does not check chain):
        //            ((IMAPSClient) imap).setTrustManager(TrustManagerUtils.getValidateServerCertificateTrustManager());
        // OR enable the next line if the server uses a self-signed certificate (no checks)
        //            ((IMAPSClient) imap).setTrustManager(TrustManagerUtils.getAcceptAllTrustManager());
    } else {
        imap = new IMAPClient();
    }
    System.out.println("Connecting to server " + server + " on " + imap.getDefaultPort());

    // We want to timeout if a response takes longer than 60 seconds
    imap.setDefaultTimeout(60000);

    // suppress login details
    imap.addProtocolCommandListener(new PrintCommandListener(System.out, true));

    try {
        imap.connect(server);
    } catch (IOException e) {
        throw new RuntimeException("Could not connect to server.", e);
    }

    try {
        if (!imap.login(username, password)) {
            System.err.println("Could not login to server. Check password.");
            imap.disconnect();
            System.exit(3);
        }

        imap.setSoTimeout(6000);

        imap.capability();

        imap.select("inbox");

        imap.examine("inbox");

        imap.status("inbox", new String[] { "MESSAGES" });

        imap.logout();
        imap.disconnect();
    } catch (IOException e) {
        System.out.println(imap.getReplyString());
        e.printStackTrace();
        System.exit(10);
        return;
    }
}

From source file:me.schiz.jmeter.protocol.imap.sampler.IMAPSampler.java

private SampleResult sampleCapability(SampleResult sr) {
    SocketClient soclient = SessionStorage.getInstance().getClient(getSOClient());
    IMAPClient client = null;
    if (soclient instanceof IMAPClient)
        client = (IMAPClient) soclient;//from www.j  a  v  a2  s .co m

    boolean success = false;
    String request = "CAPABILITY \n";
    request += "Client : " + getClient() + "\n";
    request += "Client Name : " + getClientName() + "\n";
    sr.setRequestHeaders(request);
    if (client == null) {
        clientNotFound(sr);
        return sr;
    } else {
        synchronized (client) {
            sr.sampleStart();
            try {
                success = client.capability();
                sr.setSuccessful(success);
                if (getCheckSuccessful()) {
                    sr.setSuccessful(success);
                    if (success)
                        sr.setResponseCodeOK();
                    else
                        sr.setResponseCode(RC_ERROR);
                } else
                    sr.setResponseCodeOK();
                sr.setResponseData(client.getReplyString().getBytes());
            } catch (IOException e) {
                sr.setSuccessful(false);
                sr.setResponseData(e.toString().getBytes());
                sr.setResponseCode(e.getClass().getName());
                log.error("client `" + getClient() + "` ", e);
                removeClient();
            }
            sr.sampleEnd();
        }
    }
    return sr;
}

From source file:org.apache.common.net.examples.mail.IMAPMail.java

public static final void main(String[] args) {
    if (args.length < 3) {
        System.err.println("Usage: IMAPMail <imap server hostname> <username> <password> [TLS]");
        System.exit(1);//  w  w  w  . j  ava 2 s .  com
    }

    String server = args[0];
    String username = args[1];
    String password = args[2];

    String proto = (args.length > 3) ? args[3] : null;

    IMAPClient imap;

    if (proto != null) {
        System.out.println("Using secure protocol: " + proto);
        imap = new IMAPSClient(proto, true); // implicit
        // enable the next line to only check if the server certificate has expired (does not check chain):
        //            ((IMAPSClient) imap).setTrustManager(TrustManagerUtils.getValidateServerCertificateTrustManager());
        // OR enable the next line if the server uses a self-signed certificate (no checks)
        //            ((IMAPSClient) imap).setTrustManager(TrustManagerUtils.getAcceptAllTrustManager());
    } else {
        imap = new IMAPClient();
    }
    System.out.println("Connecting to server " + server + " on " + imap.getDefaultPort());

    // We want to timeout if a response takes longer than 60 seconds
    imap.setDefaultTimeout(60000);

    // suppress login details
    imap.addProtocolCommandListener(new PrintCommandListener(System.out, true));

    try {
        imap.connect(server);
    } catch (IOException e) {
        throw new RuntimeException("Could not connect to server.", e);
    }

    try {
        if (!imap.login(username, password)) {
            System.err.println("Could not login to server. Check password.");
            imap.disconnect();
            System.exit(3);
        }

        imap.setSoTimeout(6000);

        imap.capability();

        imap.select("inbox");

        imap.examine("inbox");

        imap.status("inbox", new String[] { "MESSAGES" });

        imap.logout();
        imap.disconnect();
    } catch (IOException e) {
        System.out.println(imap.getReplyString());
        e.printStackTrace();
        System.exit(10);
        return;
    }
}

From source file:org.apache.commons.net.examples.mail.IMAPMail.java

public static void main(String[] args) throws IOException {
    if (args.length != 1) {
        System.err.println("Usage: IMAPMail imap[s]://username:password@server/");
        System.err.println("Connects to server; lists capabilities and shows Inbox status");
        System.exit(1);/*from   w  w w.  j a v  a 2  s.com*/
    }

    URI uri = URI.create(args[0]);

    // Connect and login
    final IMAPClient imap = IMAPUtils.imapLogin(uri, 10000, null);

    // suppress login details
    imap.addProtocolCommandListener(new PrintCommandListener(System.out, true));

    try {
        imap.setSoTimeout(6000);

        imap.capability();

        imap.select("inbox");

        imap.examine("inbox");

        imap.status("inbox", new String[] { "MESSAGES" });

        imap.list("", "*"); // Show the folders

    } catch (IOException e) {
        System.out.println(imap.getReplyString());
        e.printStackTrace();
        System.exit(10);
        return;
    } finally {
        imap.logout();
        imap.disconnect();
    }
}