Solved

Trapping errors when using Indy

Posted on 2004-04-29
5
429 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
  • 3
5 Comments
 
LVL 12

Expert Comment

by:Ivanov_G
Comment Utility

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

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 50 total points
Comment Utility

  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
Comment Utility

   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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

728 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now