-2

I would like to know. Is it possible to verify a email address using delphi.

I have found an article on how to do it step by step. But cannot find a way to do it through code.

Any suggestions?

http://www.labnol.org/software/verify-email-address/18220/

Update 1:

I have just found an article that explains how to do exactly what i want. But i cannot get the code to work. I keep getting Undeclared identifier 'Tmail' at line 28.

http://www.emailarchitect.net/easendmail/ex/d/11.aspx

Thank You

RRUZ
  • 134,889
  • 20
  • 356
  • 483
user3510818
  • 103
  • 1
  • 12
  • I recommend you to post all relevant data in your question and not attach them as links to other pages. – Stefan May 26 '16 at 09:49

1 Answers1

3

Delphi ships with Indy, and those steps can be performed using Indy's TIdDNSResolver and TIdSMTP components, eg:

function VerifyEmail(const email: string): Boolean;
var
  I: Integer;
  domain: string;
begin
  Result := False;

  I := Pos('@', email);
  if I = 0 then Exit;

  domain := Copy(email, I+1, MaxInt);

  IdDNSResolver1.Host := ... Host/IP of DNS server you want to query ...;
  IdDNSResolver1.QueryType := [qtMX];

  try
    IdDNSResolver1.Resolve(domain);
  except
    Exit;
  end;

  for I := 0 to IdDNSResolver1.QueryResult.Count-1 do
  begin
    if IdDNSResolver1.QueryResult[I] is TMXRecord then
    begin
      IdSMTP1.Host := TMXRecord(IdDNSResolver1.QueryResult[I]).ExchangeServer;
      try
        IdSMTP1.Connect;
        try
          IdSMTP1.SendCmd('MAIL FROM:<labnol@labnol.org>', 250);
          IdSMTP1.SendCmd('RCPT TO:<'+email+'>', [250, 251]);
          Result := True;
          Exit;
        finally
          IdSMTP1.Disconnect;
        end;
      except
      end;
    end;
  end;
end;

Alternatively, use the TIdSMTP.Verify() method instead, that way you don't have to fake an email just to discover if the recipient is accepted, eg:

function VerifyEmail(const email: string): Boolean;
var
  I: Integer;
  user, domain: string;
begin
  Result := False;

  I := Pos('@', email);
  if I = 0 then Exit;

  user := Copy(email, 1, I-1);
  domain := Copy(email, I+1, MaxInt);

  IdDNSResolver1.Host := ... Host/IP of DNS server you want to query ...;
  IdDNSResolver1.QueryType := [qtMX];

  try
    IdDNSResolver1.Resolve(domain);
  except
    Exit;
  end;

  for I := 0 to IdDNSResolver1.QueryResult.Count-1 do
  begin
    if IdDNSResolver1.QueryResult[I] is TMXRecord then
    begin
      IdSMTP1.Host := TMXRecord(IdDNSResolver1.QueryResult[I]).ExchangeServer;
      try
        IdSMTP1.Connect;
        try
          IdSMTP1.Verify(user);
          Result := True;
          Exit;
        finally
          IdSMTP1.Disconnect;
        end;
      except
      end;
    end;
  end;
end;
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Can you possibly give an explanation of what each section of code will be executing? And how accurate the result should be more or less? – user3510818 Jul 09 '14 at 19:08
  • 2
    What exactly is unclear about what I wrote? `TIdDNSResolver` is used to resolve the email address's domain (ie, `gmail.com`) to a list of SMTP server IP addresses, then `TIdSMTP` is used to check each SMTP server to see if it will accept the email address or not. If any of those commands fail, an exception is raised. – Remy Lebeau Jul 09 '14 at 19:50
  • Thank you im gona give it a try. Get back to you asap. – user3510818 Jul 19 '14 at 21:01
  • 1
    There are plenty of email addresses out there that are deliverable that fail this verification function. What does the user want? To implement a check that will return false even when the email is deliverable? This is not a good idea. – Warren P Jan 02 '18 at 20:16