How can I abort a SMTP send operation with Synapse for Delphi?

delphi, email

Solution

Synapse provides a heartbeat function, which allows implementation of Cancel behaviour.

http://www.ararat.cz/synapse/doku.php/public:howto:heartbeat

Handle the OnHeartbeat event, set the HeartbeatRate property to the interval between heartbeats, and set the StopFlag to cancel the operation.

Problem

At the moment I am testing Ararat Synapse to send e-mails in Delphi. A local function creates a TSMTPSend and sends the e-mail. How can I abort this operation? I have set a callback function assigned to SMTP.Sock.OnStatus to perform some status output. When I want to abort the send progress, I thought I could use the TTCPBlockSocket of the TSMTPSend within the callback function because in this function I have no access to the TSMTPSend directly. What I wanted to do looks basically like ``` MyCallBack(Sender: TObject; Reason: THookSocketReason; const Value: string); begin if FCancelWasClicked then begin if Sender is TTCPBlockSocket then TTCPBlockSocket(Sender).StopFlag := True; // or TTCPBlockSocket(Sender).AbortSocket or CloseSocket end; end; ``` But StopFlag shows no effect and AbortSocket/ CloseSocket lead to a StackOverFlow because the socket will then be pumping HR_CloseSocket messages endlessly. Am I doing it wrong? Are there other options?

Original source