How to validate an address of free e-mail.

How to validate an address of free e-mail.

Ex:

Free e-mail account Hotmail, gmail, ..... not to accept
ProgsXAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
TheRealLokiConnect With a Mentor Senior DeveloperCommented:
The only way to validat an email address is to send an email to it.. if they reply, or can contact you and say what was in teh email, then it is valid.

The reason I say this is that, even thought the RFC mentions a "VRFY" command for pop3, not many servers actually support this.
This is because spammers get paid based on how many "valid email addresses" they can provide.
It is in the user's / Mail server's best interest *not* to indicate whether an address is valid straight away.
Most servers will eventually send a "no such user" email response back to you, but this can take a few minutes.
This is still not an indication that the email address does "not exits" as users may be bounding the emails based on suspected spam content.
Free Email servers like hotmail, etc. still require an smtp server (usually port 25)
If you wish to deny entry of these accounts, you will need to create a list of them

Here's a simple example i wrote for you in Delphi

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls,
// used for simple domain name check
  IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdGlobal, IdException;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    function IsEmailValid(emailaddress: string; var resultstring: string): boolean;
    function CheckIfDomainISNotBlocked(domain: string;  var resultstring: string): boolean;
    function LookupDomain(domain: string; var resultstring: string): boolean;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
   resultstring: string;
begin
  if IsEmailValid(edit1.text, resultstring) then
    showmessage('valid'+#13#10 + resultstring)
  else
    showmessage('invalid'+#13#10 + resultstring);
end;

function TForm1.IsEmailValid(emailaddress: string; var resultstring: string): boolean;
var
  domain: string;
  at_index, dot_index, i: integer;
  atcount, dotcount: integer;
begin
  result := true;
  resultstring := '';
  atcount := 0;
  dotcount := 0;
  i := 1;
  while result and (i <= length(emailaddress)) do
  begin
    if emailaddress[i] = '@' then inc(atcount)
    else if not (emailaddress[i] in ['0'..'9', 'a'..'z', 'A'..'Z', '_', '.', '~']) then
    begin
      result := false;
      resultstring := 'illegal character "' + emailaddress[i] + '"';
    end;
    inc(i);
  end;
  if (atcount = 0) then
  begin
    result := false;
    resultstring := 'no "@" found';
  end
  else if (atcount > 1) then
  begin
    result := false;
    resultstring := 'Too many "@"s';
  end;
  if result then
  begin
    at_index := pos('@', emailaddress);
    result := (at_index > 1);
    if result then
    begin
      domain := copy(emailaddress, at_index + 1, maxint);
      dot_index := pos('.', domain);
      if dot_index = 0 then
      begin
        result := false;
        resultstring := 'no "." found near domain name';
      end
      else if dot_index < 2 then
      begin
        result := false;
        resultstring := 'no domain name before "."';
      end
      else if (dot_Index = length(domain)) then
      begin
        result := false;
        resultstring := '"." character can not be last character';
      end
      else
      begin
        result := CheckIfDomainISNotBlocked(domain, resultstring); // eg. hotmail.com
        if result then result := LookupDomain(domain, resultstring)
      end;
    end
    else
      resultstring := 'no name before the @ character';
  end;
end;

function TForm1.CheckIfDomainISNotBlocked(domain: string; var resultstring: string): boolean;
const
  maxnbanneddomains = 3;
  bannedDomains: array[0..(maxnbanneddomains-1)] of string = ('hotmail.com', 'yahoo.', 'gmail.com');
var
  i: integer;
begin
  result := true;
  resultstring := '';
  i := 0;
  while (result and (i < maxnbanneddomains)) do
  begin
    if (pos(bannedDomains[i], domain) > 0) then
    begin
      result := false;
      resultstring := 'banned domain: ' + bannedDomains[i];
    end
    else
      inc(i);
  end;
end;



function TForm1.LookupDomain(domain: string; var resultstring: string): boolean;
var
  TCPClientTest: TIdTCPClient;
  s: string;
begin
  result := true; //use whatever component you want to check. eg. Indy TCP client below
  resultstring := '';
// Example simple test only - if we can connect, then assume it is a valid mail server

  TCPClientTest := TIdTCPClient.Create(nil);
  try
    TCPClientTest.MaxLineAction := maException;
    TCPClientTest.Port := 25; // pop3 would be 110;
    try
      TCPClientTest.Host := domain;
      TCPClientTest.Connect(10000); // 10 second timeout
      TCPClientTest.Disconnect;
    except
      on e: exception do
      begin
        result := false;
        resultstring := 'Could not connect: ' + e.message;
      end;
    end;
  finally
    FreeAndNil(TCPClientTest);
  end;
end;

end.
0
 
ProgsXAuthor Commented:
good code

now

how to make to have to compare all the free account
0
 
TheRealLokiSenior DeveloperCommented:
change this part
const
  maxnbanneddomains = 3;
  bannedDomains: array[0..(maxnbanneddomains-1)] of string = ('hotmail.com', 'yahoo.', 'gmail.com');

Not sure where you'll get a list of "free" mail servers like that, but that's not really a delphi question
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.