I need to be able to verify that each email address is a valid one

Giganews Newsgroups
Subject: I need to be able to verify that each email address is a valid one
Posted by:  Raul Da Silva (raul…@megafreight.co.za)
Date: Wed, 6 Jul 2005

Hello Follow Programmers

I have written a mail program to send multiple emails. and a procedures that
take multiple parameters. I have included the procedures further down below.

The TmtSMTP is my own component which is basiaclly TidSMTP in a nutsheel
just with slight changes.

I then use TidMessage component to set up all the particulars for the actual
email.... (Recipients, Subject, Bosy....etc). I then send the email off.

My question to you guys and girls is :

I need to Authenticate every email address in my Recipients property. The
problem is that if one of the email address is invalid then none of the
Recipients get the email. I would like to avoid going in a loop and sending
each email out one at a time. Is there no way of checking if each email
address is valid or verify that the email address in a valid one ?

{
----------------------------------------------------------------------------
----
SendMail
----------------------------------------------------------------------------
---}
procedure SendMail(aReplyTo, aRecipients, aSenderName, aSenderAddress,
aServer, aSubject, aCc,  aBCc, aFromName, aFromAddress: String;  aBody,
aAttachment: TStrings; aHTML: Boolean);
var
smtp: TmtSMTP;
i: Integer;
begin
  smtp := TmtSMTP.Create(Self, aServer);
  try
    smtp.Server := aServer;
    smtp.Recipients := aRecipients;
    smtp.ReplyTo := aReplyTo;
    smtp.SenderName := aSenderName;
    smtp.SenderAddress := aSenderAddress;
    smtp.Subject := aSubject;
    smtp.DisplayHTML := aHTML;
    smtp.AssignBody(aBody);
    smtp.Cc := aCc;
    smtp.BCc := aBCc;
    smtp.FromName := aFromName;
    smtp.FromAddress := aFromAddress;
    //
    for i := 0 to aAttachment.Count - 1 do
    begin
      smtp.AttachedFiles.Add(aAttachment.Strings[i]);
    end;
    //
    try
      smtp.SendMessage;
    except
      MessageDlg('Cant send the email.', mtError, [mbOK], 0);
    end;
  finally
    smtp.Free;
  end;
end;

Here is the sendmessage procedure which is called above.

{
----------------------------------------------------------------------------
----
  SendMessage
----------------------------------------------------------------------------
---}
function TmtSMTP.SendMessage: Boolean;
var
i: Integer;
begin
  Result := true;
  with FSMTP do
  try
    if (Connected) then
      Disconnect;
    //
    Connect;
    try
      // Display as HTML or plain text
      if (DisplayHTML) then
        FMessage.ContentType:= 'text/HTML'
      else
        FMessage.ContentType:= 'text/plain';
      // Add attachment to list
      for i := 0 to AttachedFiles.Count -1 do
      begin
        FAttachment := TIdAttachment.Create(FMessage.MessageParts,
AttachedFiles[i]);
      end;
      //
      try
        // If connected then send mail
        if (Connected) then
          Send(FMessage);
        //
      finally
        if (Assigned(FAttachment)) then
          FAttachment.Free;
        //
      end;
      //
    finally
      Disconnect;
    end;
  except
    Result := false;
  end;
end;

Any help or comments would be greatly appreciated.

Thanks and have a wonderful day.

Best Regards

Raul

Replies