Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Trapping errors when using Indy

Posted on 2004-04-29
5
Medium Priority
?
439 Views
Last Modified: 2010-08-05

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
Comment
Question by:dealclickcouk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 10948534

  // 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
 

Author Comment

by:dealclickcouk
ID: 10948851

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

      finally
            Data.IdSMTP.Disconnect;
      end;

part?

Thanks
0
 
LVL 12

Accepted Solution

by:
Ivanov_G earned 200 total points
ID: 10948909

  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
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 10948925

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

Expert Comment

by:geobul
ID: 10949037
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question