Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Send Email to Distribution List
See more SMTP Examples
Sends the same email to a list of 1000 email addresses in 50 sends where each email has 20 recipients.Note: Chilkat is not intended nor designed for mass emailing. A solution such as this might be used for a corporate emailing to employees, or an emailing to newsletter subscribers.
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.BinData,
Chilkat.StringTable,
Chilkat.Email,
Chilkat.StringBuilder;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
mailman: TMailMan;
email: TEmail;
bdMime: TBinData;
distList: TStringTable;
sbRecipients: TStringBuilder;
i: Integer;
szDistList: Integer;
j: Integer;
begin
success := False;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
mailman := TMailMan.Create;
mailman.SmtpHost := 'smtp.mymailserver.com';
mailman.SmtpPort := 465;
mailman.SmtpSsl := True;
mailman.SmtpUsername := 'myUsername';
mailman.SmtpPassword := 'myPassword';
// Create a new email object
email := TEmail.Create;
email.Subject := 'This is a test';
email.Body := 'This is a test';
email.From := 'Senders Name <sender@example.com>';
email.AddTo('Subscribers','subscribers@example.com');
bdMime := TBinData.Create;
mailman.RenderToMimeBd(email,bdMime);
// Load a file containing one email address per line.
distList := TStringTable.Create;
success := distList.AppendFromFile(1000,'utf-8','qa_data/distList.txt');
if (success = False) then
begin
WriteLn(distList.LastErrorText);
Exit;
end;
sbRecipients := TStringBuilder.Create;
i := 0;
szDistList := distList.Count;
j := 0;
while i < szDistList do
begin
// Build a list of comma-separated recipients.
if (j > 0) then
begin
sbRecipients.Append(',');
end;
sbRecipients.Append(distList.StringAt(i));
i := i + 1;
j := j + 1;
// If we have 20 recipients, or we have the final recipient in the final chunk, then send.
if ((j = 20) or (i = szDistList)) then
begin
success := mailman.SendMimeBd('sender@example.com',sbRecipients.GetAsString(),bdMime);
if (success <> True) then
begin
WriteLn(mailman.LastErrorText);
Exit;
end;
j := 0;
sbRecipients.Clear();
end;
end;
success := mailman.CloseSmtpConnection();
if (success <> True) then
begin
WriteLn('Connection to SMTP server not closed cleanly.');
end;
WriteLn('Email sent to distirbution list.');
mailman.Free;
email.Free;
bdMime.Free;
distList.Free;
sbRecipients.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.