We have been using ICS, THtmlSmtpCli to send emails from our applications for some time now, both in the Main VCL thread and Threads. Earlier this week we discovered that it we are no longer able to send email from Threads. Sending it from the main VCL thread works fine.
So, I my question is two-fold:
- Has anybody experienced the same problem?
- Are there any other components that we could look at to send emails from Delphi Apps.
The code we use is as follows …
FRunning := True;
FHtmlSmtpClient := THtmlSmtpCli.Create(nil);
with FHtmlSmtpClient do
begin
Port := '25';
Host := FHost;
AuthType := smtpAuthNone;
ConfirmReceipt := FReadReceipt;
HdrPriority := smtpPriorityNone;
ContentType := smtpHtml;
FromName := FFromAddr;
HdrFrom := FFromAddr;
HdrTo := FToAddr;
HdrSubject := FSubject;
OnCommand := FHtmlSmtpClientCommand;
OnRequestDone := FHtmlSmtpClientRequestDone;
OnSessionClosed := FHtmlSmtpClientSessionClosed;
{ Start first operation to do to send an email }
{ Next operations are started from OnRequestDone event }
Connect;
end;
//Process the requests to send the email
procedure FHtmlSmtpClientRequestDone(Sender: TObject; RqType: TSmtpRequest; ErrorCode: word);
begin
if not FRunning then
Exit;
{ Start next operation, but first check if previous one was OK }
if ErrorCode <> 0 then
begin
FRunning := FALSE; { Terminate All-In-One demo }
Exit;
end;
case RqType of
smtpConnect:
begin
if FHtmlSmtpClient.AuthType = smtpAuthNone then
FHtmlSmtpClient.Helo
else
FHtmlSmtpClient.Ehlo;
end;
smtpHelo: FHtmlSmtpClient.MailFrom;
smtpEhlo: FHtmlSmtpClient.Auth;
smtpAuth: FHtmlSmtpClient.MailFrom;
smtpMailFrom: FHtmlSmtpClient.RcptTo;
smtpRcptTo: FHtmlSmtpClient.Data;
smtpData: FHtmlSmtpClient.Quit;
smtpQuit: FRunning := FALSE;
end;
end;
Regards, Pieter.
To answer your second question, you can use Indy, which ships with Delphi. It has
TIdMessageandTIdSMTPcomponents, and aTIdMessageBuilderHtmlutility class.