Sample code for 30+ languages & platforms
Delphi DLL

Log In to an IMAP Server

See more IMAP Examples

Demonstrates the Chilkat Imap.Login method, which authenticates the connected IMAP session using a login name and password. Connect must be called first. This example connects, logs in, and disconnects.

Background: Authentication is a distinct step from connecting: after the TCP/TLS connection is up, Login sends the credentials so the server will grant access to the account's mailboxes. The exact mechanism (plain LOGIN, SASL, or OAuth2) depends on the server and the AuthMethod setting. For providers that require OAuth2 (such as Gmail and Outlook.com), the password is an access token rather than an account password.

Chilkat Delphi DLL Downloads

Delphi DLL
var
success: Boolean;
imap: HCkImap;

begin
success := False;

//  Demonstrates the Imap.Login method, which authenticates the connected IMAP session using
//  a login name and password.  Call Connect first.

imap := CkImap_Create();

CkImap_putSsl(imap,True);
CkImap_putPort(imap,993);

//  Connect before logging in.
success := CkImap_Connect(imap,'imap.example.com');
if (success = False) then
  begin
    Memo1.Lines.Add(CkImap__lastErrorText(imap));
    Exit;
  end;

//  Authenticate with the login name and password.
success := CkImap_Login(imap,'user@example.com','myPassword');
if (success = False) then
  begin
    Memo1.Lines.Add(CkImap__lastErrorText(imap));
    Exit;
  end;
Memo1.Lines.Add('Logged in to the IMAP server.');

success := CkImap_Disconnect(imap);
if (success = False) then
  begin
    Memo1.Lines.Add(CkImap__lastErrorText(imap));
    Exit;
  end;

CkImap_Dispose(imap);