Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Send an Email via SMTP
See more SMTP Examples
Demonstrates the Chilkat MailMan.SendEmail method, which sends a single Email object through the configured SMTP server. This example configures the SMTP connection, builds a message, and sends it.
Background: SMTP (Simple Mail Transfer Protocol) is the protocol for sending mail. The typical flow is: connect to the server, authenticate, then hand over the message with
MAIL FROM, RCPT TO, and DATA. SendEmail wraps all of that: you build an Email with subject, recipients, and body, and Chilkat renders it to MIME and delivers it. Modern submission commonly uses implicit TLS on port 465 (SmtpSsl = true).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,
Chilkat.Email;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
mailman: TMailMan;
email: TEmail;
begin
success := False;
// Demonstrates the MailMan.SendEmail method, which sends a single Email object through the
// configured SMTP server.
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';
// Build the email to send.
email := TEmail.Create;
email.Subject := 'Test email from Chilkat';
email.From := 'alice@example.com';
email.AddTo('Bob','bob@example.com');
email.Body := 'Hello, this is a test message.';
// Send the email.
success := mailman.SendEmail(email);
if (success = False) then
begin
WriteLn(mailman.LastErrorText);
Exit;
end;
WriteLn('Email sent.');
// Note: Explicitly connecting/authenticating is optional. Chilkat MailMan automatically
// connects and authenticates -- using the property settings above -- whenever a server
// operation requires it. Calling the explicit connect/authenticate methods can still be
// helpful to determine whether a failure occurs while connecting or while authenticating.
mailman.Free;
email.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.