Link to home
Start Free TrialLog in
Avatar of dealclickcouk
dealclickcouk

asked on

Trapping errors when using Indy


Hi

I'm using Delphi 5 with indy to send emails, I have code like below, but it doesn't trap errors, sometimes get socket failure.

How do I modify the function below so that I can pass back success or failure when called?

Function SendEMailwAttach (Address, Text, Subject, FileName: string): string;
begin

      Data.IdMessage.Recipients.EMailAddresses := Address;
      Data.IdMessage.Body.Text := Text;
      Data.IdMessage.Subject := Subject;
  TidAttachment.Create(Data.IdMessage.MessageParts, FileName);

      Data.IdMessage.From.Text := Data.UserName.AsString;
      Data.IdMessage.Organization := Data.DefaultsFailedOrganisation.AsString;
      Data.IdMessage.ContentType := 'text/html';

      Data.IdSMTP.Host := Data.DefaultsEMailHostIp.AsString;
      Data.IdSMTP.Connect;
      try
            Data.IdSMTP.Send (Data.IdMessage);
      finally
            Data.IdSMTP.Disconnect;
      end;
end;
Avatar of Ivanov_G
Ivanov_G
Flag of Bulgaria image


  // handle the connection
  try
     Data.IdSMTP.Connect;
  except
     on E: Exception do
        begin
           MessageDlg(E.Message, mtError, [mbOK], 0);
           Exit;
        end;
  end;

  // handle message sending
  try
     Data.IdSMTP.Send (Data.IdMessage);
  except
     on E: Exception do
        begin
           MessageDlg(E.Message, mtError, [mbOK], 0);
           Exit;
        end;
   end;
Avatar of dealclickcouk
dealclickcouk

ASKER


Thanks with the second on the sending where do I put the

      finally
            Data.IdSMTP.Disconnect;
      end;

part?

Thanks
ASKER CERTIFIED SOLUTION
Avatar of Ivanov_G
Ivanov_G
Flag of Bulgaria image

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

   this is if Connect or Send methods fail, you will show the MessageDlg and Exit the function.
   the finally statement is always executed ...
Hi,

You may use the result of the function to pass success or failure back:

Function SendEMailwAttach (Address, Text, Subject, FileName: string): boolean; // instead of string
begin
  result := true; // optimistic
  try
     Data.IdMessage.Recipients.EMailAddresses := Address;
     Data.IdMessage.Body.Text := Text;
     Data.IdMessage.Subject := Subject;
     TidAttachment.Create(Data.IdMessage.MessageParts, FileName);

     Data.IdMessage.From.Text := Data.UserName.AsString;
     Data.IdMessage.Organization := Data.DefaultsFailedOrganisation.AsString;
     Data.IdMessage.ContentType := 'text/html';

     Data.IdSMTP.Host := Data.DefaultsEMailHostIp.AsString;
     Data.IdSMTP.Connect;
     try
          Data.IdSMTP.Send (Data.IdMessage);
     finally
          Data.IdSMTP.Disconnect;
     end;
  except
    result := false; // any error = failure
  end;
end;

and use it like:

if  SendEMailwAttach(...) then ShowMessage('Mail has been sent successfuly')
else ShowMessage('Error sending the mail');

Regards, Geo