Jump to content
philipp.hofmann

Send Mail synchron via ICS

Recommended Posts

Hi,

my current Indy implementation sends email synchron, so my method is waiting until the mail is sent and then showing a result.
It's not clear for me how I can achieve this with ICS-code as after I've requested connect, the app is frozen, even if I request connect within a thread.
Is this achievable to avoid bigger changes on our code while migrating from Indy to ICS?

 

class function TEMailUtils.sendEmail(receiver,subject,body,attachment:String;IdIOHandler:TIdSSLIOHandlerSocketOpenSSL;doNotReply:boolean):boolean;
begin
  finished:=false;
  if (sslSmtpClient=nil) then
  begin
    sslSmtpClient:=TSSLSmtpCli.create(nil);
    sslContext:=TSSLContext.create(nil);
    sslSmtpClient.SslContext:=sslContext;
    SslSmtpClient.Host:='smtp.xxxxxx.de';
    SslSmtpClient.Port:='465';
    SslSmtpClient.AuthType:=TSmtpAuthType.smtpAuthLogin;
    SslSmtpClient.SslType:=TSmtpSslType.smtpTlsImplicit;
    SslSmtpClient.Username:='xxxxxxxx@xxxxxxxx.com';
    SslSmtpClient.Password:='xxxxxxxxxxxxx';
    SslSmtpClient.OnRequestDone:=SslSmtpClientRequestDone;
  end;
  if (doNotReply) then
    SslSmtpClient.FromName:='doNotReply (icTrainer)'
  else
    SslSmtpClient.FromName:='icTrainer';
  SslSmtpClient.HdrFrom:=SslSmtpClient.FromName;
  SslSmtpClient.HdrTo         := receiver;
  SslSmtpClient.RcptName.Clear;
  SslSmtpClient.RcptNameAdd(receiver,'','');
  SslSmtpClient.HdrSubject    := subject;
  SslSmtpClient.MailMessage:=TStringUtils.getTStringList(body,'|');
  SslSmtpClient.EmailFiles.Add(attachment);
  mlog.info('start sendMail');
  SslSmtpClient.Connect;
  while (not finished) do
    sleep(100);
  result:=resultVal;
  mlog.info('finished sendMail: '+TStringUtils.BoolToStr(resultVal));
end;

class procedure TEMailUtils.SslSmtpClientRequestDone(Sender : TObject; RqType : TSmtpRequest; Error  : Word);
begin
    mlog.info('RequestDone Rq=' + IntToStr(Ord(RqType)) + ' Error='+ IntToStr(Error));
    if (Error <> 0) then
    begin
      mlog.info('Can`t send mail: RequestDone Rq=' + IntToStr(Ord(RqType)) +' Error='+ SslSmtpClient.ErrorMessage);
      finished:=true;
      Exit;
    end;
    case RqType of
    smtpConnect:  SslSmtpClient.Ehlo;
    smtpEhlo:     SslSmtpClient.Auth;
    smtpAuth:     SslSmtpClient.MailFrom;
    smtpMailFrom: SslSmtpClient.RcptTo;
    smtpRcptTo:   SslSmtpClient.Data;
    smtpData:     begin
                    mlog.info('Send Mail and quit');
                    resultVal:=true;
                    finished:=true;
                    SslSmtpClient.Quit;
                  end;
    end;
end;

 

Share this post


Link to post

Please read my comments about using the TIcsMailQueue component in a topic in this conference two weeks ago about sending HTML mail.

 

Angus

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×