Sample code for 30+ languages & platforms
Java

Connect to an IMAP Server

See more IMAP Examples

Demonstrates the Chilkat Imap.Connect method, which establishes a TCP connection to the IMAP server but does not authenticate. The argument may be a hostname, IPv4, or IPv6 address; configure Ssl and Port first. This example connects with implicit TLS, then logs in and disconnects.

Background: IMAP (Internet Message Access Protocol) is the protocol for accessing mail that stays on the server — unlike POP3, which typically downloads and removes it. That server-side model is what lets multiple devices see the same mailboxes, folders, and read/unread state. A session starts by connecting (usually implicit TLS on port 993), then authenticating — Connect performs only the first step, so it is always followed by Login.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Imap;

public class ChilkatExample {

    public static void main(String[] args) {
        // Demonstrates the Imap.Connect method, which establishes a TCP connection to the IMAP
        // server but does not authenticate.  Call Login after connecting to authenticate.

        Imap imap = new Imap();

        // Use implicit TLS on the standard IMAPS port.
        imap.setSsl(true);
        imap.setPort(993);

        // Establish the connection to the IMAP server (no authentication yet).
        try {
            imap.connect("imap.example.com");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("Connected to the IMAP server.");

        // Authenticate the session.
        try {
            imap.login("user@example.com", "myPassword");

            // Disconnect when finished.
            imap.disconnect();
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }
    }
}