当然可以
只要从TThread派生新的线程类,结合TClientSocket就行了
type
TSendMailThread=class(TThread)
private
Fsmtp:TIdSMTP;
//procedure OnConnectionError(Sender : TObject);
{ Private declarations }
protected
procedure Execute;
override;
procedure Terminate;
public
constructor Create(CreateSuspended: Boolean);
end;
{ TSendMailThread }
constructor TSendMailThread.Create(CreateSuspended: Boolean);
begin
inherited Create(CreateSuspended);
Priority := tpIdle;
self.FreeOnTerminate:=true;
Fsmtp:=TIdSMTP.Create(FormMain.IdSMTP1);
Fsmtp.Authen
ticationType:=atLogin;
Fsmtp.Host:=FormMain.Edit1.Text;
Fsmtp.Port:=StrToInt(FormMain.Edit2.Text);
Fsmtp.UserID:=formMain.Edit4.Text;
//Fsmtp.OnWorkEnd:=OnConnectionError;
end;
procedure TSendMailThread.Terminate;
begin
if fsmtp.Connected=True then
Fsmtp.Disconnect;
end;
{procedure TSendMailThread.OnConnectionError(Sender:TObject);
begin
self.DoTerminate;
end;
}
procedure TSendMailThread.Execute;
begin
if (self.Terminated=false) then
begin
formmain.IdMessage1.SetBody(Formmain.Memo1.Lines);
formmain.IdMessage1.From.Text :='Codehunter@sohu.com';
formmain.IdMessage1.Recipients.EMailAddresses:=formmain.ListBox1.Items.Strings[0];
formmain.IdMessage1.Subject:='password';
Fsmtp.Connect;
if Fsmtp.Connected=true then
begin
try
FSmtp.Send(formmain.IdMessage1);
except
FSmtp.Disconnect;
end;
end;
Fsmtp.Destroy;
end;
end;