In previous section, I introduced how to use asynchronous mode. In this section, I will introduce how to send mass email with multiple threads in Delphi.
FastSender object has an inner threading pool based on MaxThreads count. Firstly, Send method submits email to FastSender mail queue. Secondly threading pool retrieves email from mail queue and sends it out. Finally OnSent event informs that the email was sent successfully or unsuccessfully.
No. of worker threads in the threading pool of FastSender object is automatically adjusted based on the actual usage. The maximum no. of worker threads is up to the value of MaxThread property specified.
Note
Remarks: All of samples in this section are based on first section: Send email in a simple Delphi project. To compile and run the following example codes successfully, please click here to learn how to create the test project and add reference of EASendMail to your project.
The following example codes demonstrate how to send mass email with multiple threads in Delphi.
Note
To get the full sample projects, please refer to Samples section.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, EASendMailObjLib_TLB; // add EASendMail unit
type
TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
// FastSender event handler
procedure OnSent(ASender: TObject; lRet: Integer; const ErrDesc: WideString;
nKey: Integer; const tParam: WideString;
const senderAddr: WideString;
const Recipients: WideString);
private
{ Private declarations }
public
{ Public declarations }
end;
const
ConnectNormal = 0;
ConnectSSLAuto = 1;
ConnectSTARTTLS = 2;
ConnectDirectSSL = 3;
ConnectTryTLS = 4;
var
Form1: TForm1;
m_oFastSender: TFastSender;
implementation
{$R *.dfm}
procedure TForm1.OnSent(ASender: TObject; lRet: Integer; const ErrDesc: WideString;
nKey: Integer; const tParam: WideString;
const senderAddr: WideString;
const Recipients: WideString);
begin
if lRet = 0 then
ShowMessage(Format('%d email was sent successfully', [nKey]))
else
ShowMessage(Format('%d: faile to send email: ' + ErrDesc, [nKey]));
end;
procedure TForm1.Button1Click(Sender: TObject);
var
oSmtp: TMail;
RecipientAddr: array[0..2] of WideString;
i: Integer;
begin
if m_oFastSender = nil then
begin
m_oFastSender := TFastSender.Create(Application);
oSmtp := TMail.Create(Application);
oSmtp.LicenseCode := 'TryIt';
// Attach FastSender event handler
m_oFastSender.OnSent := OnSent;
// Set maximum number of worker threads
m_oFastSender.MaxThreads := 10;
end;
// Set your sender email address
oSmtp.FromAddr := 'test@emailarchitect.net';
// Set recpient addresses
RecipientAddr[0] := 'test@adminsystem.com';
RecipientAddr[1] := 'test1@adminsystem.com';
RecipientAddr[2] := 'test2@adminsystem.com';
// Your SMTP server address
oSmtp.ServerAddr := 'smtp.emailarchitect.net';
// User and password for ESMTP authentication, if your server doesn't require
// user authentication, please remove the following codes
oSmtp.UserName := 'test@emailarchitect.net';
oSmtp.Password := 'testpassword';
// ConnectTryTLS means if server supports SSL/TLS connection, SSL/TLS is used automatically
oSmtp.ConnectType := ConnectTryTLS;
// If your server uses 587 port
// oSmtp.ServerPort := 587;
// If your server uses 25/587/465 port with SSL/TLS
// oSmtp.ConnectType := ConnectSSLAuto;
// oSmtp.ServerPort := 587; // 25 or 587 or 465
for i := 0 to 2 do
begin
oSmtp.ClearRecipient();
oSmtp.AddRecipientEx(RecipientAddr[i], 0);
oSmtp.Subject := 'mass email test subject';
oSmtp.BodyText := 'mass email test body from delphi';
m_oFastSender.Send(oSmtp.DefaultInterface, i, RecipientAddr[i])
end;
end;
end.
Next Section
At next section I will introduce how to send email using EASendMail Service Queue in Delphi.
Appendix
Comments
If you have any comments or questions about above example codes, please click here to add your comments.