[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 440
  • Last Modified:

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;
0
dealclickcouk
Asked:
dealclickcouk
  • 3
1 Solution
 
Ivanov_GCommented:

  // 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;
0
 
dealclickcoukAuthor Commented:

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

      finally
            Data.IdSMTP.Disconnect;
      end;

part?

Thanks
0
 
Ivanov_GCommented:

  I think your function should look like this :

  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;
     try
        try
           Data.IdSMTP.Connect;
        except
           MessageDlg(E.Message, mtError, [mbOK], 0);
           Exit;
        end;
        try
          Data.IdSMTP.Send (Data.IdMessage);
        except
           MessageDlg(E.Message, mtError, [mbOK], 0);
           Exit;
        end;
     finally
          Data.IdSMTP.Disconnect;
     end;
end;
0
 
Ivanov_GCommented:

   this is if Connect or Send methods fail, you will show the MessageDlg and Exit the function.
   the finally statement is always executed ...
0
 
geobulCommented:
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
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now