Sample code for 30+ languages & platforms
Delphi DLL

Check the IMAP Socket Connection

See more IMAP Examples

Demonstrates the Chilkat Imap.CheckConnection method, which checks whether the underlying TCP socket is currently connected to the IMAP server. This performs a low-level socket-state check and does not send an IMAP command. This example connects and checks the socket.

Background: CheckConnection sits between the cached IsConnected and a full protocol round trip: it inspects the actual socket without sending an IMAP command, so it can catch a locally-closed socket that the cached flag would miss. It still cannot detect every half-open connection, though — only a real command like Noop that expects a server reply proves the session is genuinely alive end to end.

Chilkat Delphi DLL Downloads

Delphi DLL
var
success: Boolean;
imap: HCkImap;
stillConnected: Boolean;

begin
success := False;

//  Demonstrates the Imap.CheckConnection method, which checks whether the underlying TCP
//  socket is currently connected to the IMAP server.  This is a low-level socket-state check
//  and does not send an IMAP command.

imap := CkImap_Create();

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

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

//  Perform a low-level check of the socket connection state.
stillConnected := CkImap_CheckConnection(imap);
if (stillConnected = True) then
  begin
    Memo1.Lines.Add('The socket is still connected.');
  end
else
  begin
    Memo1.Lines.Add('The socket is no longer connected.');
  end;

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

CkImap_Dispose(imap);