Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

Send an SMTP NOOP Command

See more SMTP Examples

Demonstrates the Chilkat MailMan.SmtpNoop method, which sends an SMTP NOOP command to the server. NOOP does nothing except elicit a positive response, which is useful for keeping a connection alive or verifying it is still responsive. This example opens an SMTP connection, sends a NOOP, and closes.

Background: When holding an SMTP connection open across many sends (see OpenSmtpConnection), an idle stretch can cause the server to time out and drop the socket. A periodic NOOP keeps the session active and confirms the server is still responding, so the next SendEmail doesn't fail on a silently-closed connection.

Chilkat Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.MailMan;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  mailman: TMailMan;

begin
  success := False;

  //  Demonstrates the MailMan.SmtpNoop method, which sends an SMTP NOOP command to the server.
  //  NOOP does nothing except elicit a positive response, useful for keeping a connection
  //  alive or verifying it is still responsive.

  mailman := TMailMan.Create;

  //  Configure the SMTP server connection.
  mailman.SmtpHost := 'smtp.example.com';
  mailman.SmtpPort := 465;
  mailman.SmtpSsl := True;
  mailman.SmtpUsername := 'user@example.com';
  mailman.SmtpPassword := 'myPassword';

  //  Open the SMTP connection.

  success := mailman.OpenSmtpConnection();
  if (success = False) then
    begin
      WriteLn(mailman.LastErrorText);
      Exit;
    end;

  //  Send a NOOP to keep the connection alive.
  success := mailman.SmtpNoop();
  if (success = False) then
    begin
      WriteLn(mailman.LastErrorText);
      Exit;
    end;

  success := mailman.CloseSmtpConnection();
  if (success = False) then
    begin
      WriteLn(mailman.LastErrorText);
      Exit;
    end;

  WriteLn('SMTP NOOP succeeded.');


  mailman.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.