Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Open a Persistent SMTP Connection
See more SMTP Examples
Demonstrates the Chilkat MailMan.OpenSmtpConnection method, which explicitly opens a connection to the SMTP server and authenticates if a username and password, OAuth2 token, or other applicable settings have been provided. This example opens the connection, notes where sending would occur, and closes it.
Background: By default, sending a single email opens a connection, sends, and closes it. When sending many messages, that per-message overhead adds up. Explicitly calling
OpenSmtpConnection once, sending in a loop, then CloseSmtpConnection keeps a single authenticated session open for the whole batch — substantially faster and gentler on the server than reconnecting each time.Chilkat Pascal (Lazarus/Delphi) Downloads
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.OpenSmtpConnection method, which explicitly opens a connection
// to the SMTP server and authenticates if credentials (or an OAuth2 token) have been
// provided.
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';
// Explicitly open (and authenticate) the SMTP connection.
success := mailman.OpenSmtpConnection();
if (success = False) then
begin
WriteLn(mailman.LastErrorText);
Exit;
end;
WriteLn('SMTP connection opened.');
// ... send one or more emails here ...
// Close the connection when done.
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.