[twsocket] How to validate an e-mail?

Stephen Williams SWilliams@fm.optus.net.au
Fri, 2 Jun 2000 09:22:17 +1000


> Is there any way to validate an e-mail adress?
> 
> something like the demo program "httpchk".

If you just want to validate that the email address is of the correct
syntax, then this function will help.

{ Returns True if the input matches the mask.  Uses a recursive method
  to perform the check.  }
function MatchMask(Input, Mask: PChar): Boolean;
begin
  if StrComp(Hostmask, '*') = 0 then
    Result := True
  else if (Address^ = #0) and (Hostmask^ <> #0) then
    Result := False
  else if (Address^ = #0) then
    Result := True
  else
    case Hostmask^ of
      '*':
            if MatchHostmask(Address, Hostmask + 1) then
              Result := True
            else
              Result := MatchHostmask(Address + 1, Hostmask);
      '?':
            Result := MatchHostmask(Address + 1, Hostmask + 1);
    else
      if Address^ = Hostmask^ then
        Result := MatchHostmask(Address + 1, Hostmask + 1)
      else
        Result := False;
    end;
end;

Call this function with the email address and a mask of '*@*.*', such as

  if MatchMask(PChar(EmailEdit.Text), '*@*.*') then
  begin
    ...
  end;

If you want to check that the email address actually exists, then the only
way to do that is to actually send an email to that address.  If it bounces,
it doesn't exist.

Steve 'Sly' Williams
IT Development
Cable & Wireless Optus Outsourcing Services
swilliams@fm.optus.net.au
------------------------------------------------------------
"I read somewhere that 77 per cent of all the mentally ill live in poverty.
Actually, I'm more intrigued by the 23 per cent who are apparently doing
quite well for themselves."
------------------------------------------------------------