Solved

How to validate an address of free e-mail.

Posted on 2007-04-01
3
335 Views
Last Modified: 2010-04-05
How to validate an address of free e-mail.

Ex:

Free e-mail account Hotmail, gmail, ..... not to accept
0
Comment
Question by:ProgsX
  • 2
3 Comments
 
LVL 17

Accepted Solution

by:
TheRealLoki earned 250 total points
ID: 18833300
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
 

Author Comment

by:ProgsX
ID: 18833439
good code

now

how to make to have to compare all the free account
0
 
LVL 17

Expert Comment

by:TheRealLoki
ID: 18833729
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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

830 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