Sample code for 30+ languages & platforms
Java Requires Chilkat v11.0.0+

IMAP using SOCKS5, SOCKS4 Proxy

Demonstrates how to connect to an IMAP server through a SOCKS5 or SOCKS4 proxy.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Email;
import com.chilkatsoft.chilkat2.EmailBundle;
import com.chilkatsoft.chilkat2.Imap;
import com.chilkatsoft.chilkat2.MessageSet;

public class ChilkatExample {

    public static void main(String[] args) {
        // This example assumes the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        Imap imap = new Imap();

        // To use a SOCKS4 or SOCKS5 proxy, simply set the following
        // properties prior to connecting with the IMAP server:
        // The SOCKS hostname may be a domain name or
        // IP address:
        imap.setSocksHostname("www.mysocksproxyserver.com");
        imap.setSocksPort(1080);
        imap.setSocksUsername("myProxyLogin");
        imap.setSocksPassword("myProxyPassword");
        // Set the SOCKS version to 4 or 5 based on the version
        // of the SOCKS proxy server:
        imap.setSocksVersion(5);
        // Note: SOCKS4 servers only support usernames without passwords.
        // SOCKS5 servers support full login/password authentication.

        // Connect to an IMAP server through the SOCKS proxy.
        // Use TLS
        imap.setSsl(true);
        imap.setPort(993);
        try {
            imap.connect("imap.example.com");

            // Login
            imap.login("myLogin", "myPassword");

            // Select an IMAP mailbox
            imap.selectMailbox("Inbox");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // Get the message IDs of all the emails in the mailbox
        // We can choose to fetch UIDs or sequence numbers.
        boolean fetchUids = true;
        MessageSet messageSet = new MessageSet();
        try {
            imap.queryMbx("ALL", fetchUids, messageSet);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // Fetch the emails into a bundle object:
        EmailBundle bundle = new EmailBundle();
        boolean headersOnly = false;
        try {
            imap.fetchMsgSet(headersOnly, messageSet, bundle);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // Loop over the bundle and display the FROM and SUBJECT of each.
        Email email = new Email();
        int i = 0;
        int numEmails = bundle.getMessageCount();
        while (i < numEmails) {
            bundle.emailAt(i, email);

            System.out.println(email.getFrom());
            System.out.println(email.getSubject());
            System.out.println("--");
            i = i + 1;
        }

        // Disconnect from the IMAP server.
        imap.disconnect();
    }
}