Solved

Trapping errors when using Indy

Posted on 2004-04-29
5
434 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 50 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

691 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