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

Send a Raw SMTP Command

See more SMTP Examples

Demonstrates the Chilkat MailMan.SmtpSendRawCommand method, which sends a raw command to the SMTP server and returns the server's response. The second argument specifies the charset to use if the command contains non-US-ASCII characters, and the third indicates whether to base64-encode the command before sending. This example sends a raw NOOP on an open connection.

Background: SMTP is a line-based text protocol (EHLO, MAIL FROM, RCPT TO, DATA, QUIT), so a raw-command hook lets you issue anything Chilkat doesn't wrap — a server extension, or a custom step during a manual auth exchange. The base64 flag exists because some SMTP exchanges (notably AUTH challenge/response) require the argument to be base64-encoded on the wire.

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;
  response: string;

begin
  success := False;

  //  Demonstrates the MailMan.SmtpSendRawCommand method, which sends a raw command to the SMTP
  //  server and returns the server's response.  The 2nd argument is the charset used if the
  //  command contains non-US-ASCII characters, and the 3rd indicates whether to base64-encode
  //  the command before sending.

  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';

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

  //  Send the raw SMTP NOOP command (not base64-encoded).
  response := mailman.SmtpSendRawCommand('NOOP','us-ascii',False);
  if (mailman.LastMethodSuccess = False) then
    begin
      WriteLn(mailman.LastErrorText);
      Exit;
    end;
  WriteLn('NOOP response: ' + response);

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


  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.