Link to home
Start Free TrialLog in
Avatar of Stef Merlijn
Stef MerlijnFlag for Netherlands

asked on

Handle exception during sending of mailing (IdSMTP, INDY)

Hi,

The following code I use for sending E-mail within a thread.
As there are serveral things that might go wrong I want to supply the enduser with a informative message about the error and also handle accordingly.

Stuff that needs to be coverred:
Losing internetconnection
Losing networkconnection
Wrong SMTP connection parameters
Wrong Emailadresses
Any other SMTP-errors (like server rejects mailing for any reason)


Can anybody give advise about handling these errors correctly. Also supply me with errorcodes en exception-types, etc..
procedure TMailingPerEmailThread.Execute;
var IdSMTP : TIdSMTP;
    IdMessage : TIdMessage;
    i, j, MaxEmailsPerBatch, TijdTussenBatches : Integer;
begin
  MailingPerEmailIsBezig := True;
  MaxEmailsPerBatch := DM.QGebruikers.FieldByName('EmailLimietWebHost').AsInteger;
  TijdTussenBatches := DM.QGebruikers.FieldByName('EmailLimietInterval').AsInteger;
  try
    IdSMTP := TIdSMTP.Create(nil);
    IdSMTP.Host     := DM.QGebruikers.FieldByName('SMTPHost').AsString;
    IdSMTP.Port     := DM.QGebruikers.FieldByName('SMTPPoort').AsInteger;         // smtp is normaal gesproken op poort 25
    IdSMTP.Password := XTeaDecryptStr(HexToString(DM.QGebruikers.FieldByName('SMTPWachtwoord').AsString), TiGiDkCeHc);
    IdSMTP.Username := DM.QGebruikers.FieldByName('SMTPGebruikersnaam').AsString; 

    if not IdSMTP.Connected then
      IdSMTP.Connect;

    HuidigeEmail     := '';
    MailingVerzonden := 0;
    MailingDoublures := 0;
    try
      IdMessage := TIdMessage.Create(nil);
      IdMessage.AttachmentEncoding := 'MIME';
      IdMessage.ConvertPreamble    := True;
      IdMessage.Encoding           := meDefault;
      IdMessage.Subject            := FOnderwerp;
      IdMessage.From.Address       := FVan;
      // Bijlage toevoegen
      If (FBijlage <> '') then
        TIdAttachmentFile.Create(IdMessage.MessageParts, FBijlage);

      // Klantafhankelijke gegevens per E-mail
      for i := 1 to DM.QMailingPerEmail.RecordCount do
      begin
        if (ANSICompareText(HuidigeEmail, DM.QMailingPerEmail.fieldbyname('Email').Text) <> 0) then
        begin
          IdMessage.Recipients.EMailAddresses := DM.QMailingPerEmail.FieldByName('Email').Text;
          FBodyKopie := FBody;

          for j := 0 to pred(TIdMessage(IdMessage).MessageParts.Count) do
          begin
            if TIdMessage(IdMessage).MessageParts[j] is TIdText then
            begin
              TIdMessage(IdMessage).MessageParts[j].Free;
            end;
          end;

          // E-mail body in Tekst
          With TIdText.Create(IdMessage.MessageParts, nil) do
          begin
            Body.Text := FBodyKopie;
            ContentType := 'text/plain';
          end;

          try
            // Verstuur de E-mail
            IdSMTP.send(IdMessage);
            MailingVerzonden := MailingVerzonden + 1;

            // Wachten bij opgegeven Limiet per mailing
            If (MaxEmailsPerBatch > 0) then
            begin
              if (MaxEmailsPerBatch = MailingVerzonden) then
                Sleep(TijdTussenBatches * 1000);
            end;
          except
            on E: EIdSMTPReplyError do
            begin
              // Re-connect the IdSMTP
              IdSMTP.Disconnect;
              Sleep(5000);     
              // Re-connect
              IdSMTP.Connect;
              try
                // Verstuur de E-mail opnieuw
                IdSMTP.send(IdMessage);
                MailingVerzonden := MailingVerzonden + 1;

                // Wachten bij opgegeven Limiet per mailing
                If (MaxEmailsPerBatch > 0) then
                begin
                  if (MaxEmailsPerBatch = MailingVerzonden) then
                    Sleep(TijdTussenBatches * 1000);
                end;
              except
                on E: EIdSMTPReplyError do
                begin
                  // Thread afbreken.
                  Raise;
                  Break; // De loop afbreken
                end;
              end;
            end;
            on E:Exception do
            begin
              // Thread afbreken.
              Raise;
              Break; // De loop afbreken
            end;
          end;
        end else
        begin
          MailingDoublures := MailingDoublures + 1;
        end;
        DM.QMailingPerEmail.Next;
      end;
    finally
      FreeAndNil(IdMessage);
    end;
  except
    on E: Exception do
    begin
      MyMessageDlg(Format(wzzMessageFout
                        , [ConstantP])
                 , Format(wzzMailingPerEmailFout
                        , [E.Message])
                 , dkFout, NMV);
      if IdSMTP.Connected then
        IdSMTP.Disconnect;
      MailingPerEmailIsBezig := False;
      Raise;
    end;
  end;
  if IdSMTP.Connected then
    IdSMTP.Disconnect;
  IdSMTP.Free;
  MailingPerEmailIsBezig := False;
end;

Open in new window

SOLUTION
Avatar of Member_2_5194534
Member_2_5194534

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial