| 1 | unit mailchck;
|
|---|
| 2 |
|
|---|
| 3 | interface
|
|---|
| 4 |
|
|---|
| 5 | uses
|
|---|
| 6 | dnssend, smtpsend, synautil, classes, synamisc;
|
|---|
| 7 |
|
|---|
| 8 | function mailcheck(email:string):integer;
|
|---|
| 9 |
|
|---|
| 10 | implementation
|
|---|
| 11 |
|
|---|
| 12 | {
|
|---|
| 13 | 0 - address exists
|
|---|
| 14 | 1 - address may exists
|
|---|
| 15 | 2 - your DNS nannot working (cannot check!)
|
|---|
| 16 | 3 - your DNS is not defined (cannot check!)
|
|---|
| 17 | 4 - cannot contact any MX servers (cannot check!);
|
|---|
| 18 | 5 - domain not have MX record
|
|---|
| 19 | 6 - address not exists
|
|---|
| 20 | 7 - address is bad!
|
|---|
| 21 | }
|
|---|
| 22 |
|
|---|
| 23 | function mailcheck(email:string):integer;
|
|---|
| 24 | var
|
|---|
| 25 | smtp:TSMTPsend;
|
|---|
| 26 | domain:string;
|
|---|
| 27 | user: string;
|
|---|
| 28 | mailservers:tstringlist;
|
|---|
| 29 | dnsservers:tstringlist;
|
|---|
| 30 | x: integer;
|
|---|
| 31 | n, m: integer;
|
|---|
| 32 | b: boolean;
|
|---|
| 33 | begin
|
|---|
| 34 | result:=7;
|
|---|
| 35 | email:=getemailaddr(email);
|
|---|
| 36 | x := pos('@', email);
|
|---|
| 37 | if x <= 0 then
|
|---|
| 38 | exit; //invalid address format
|
|---|
| 39 | domain:=separateright(email,'@');
|
|---|
| 40 | user:=separateLeft(email,'@');
|
|---|
| 41 | if (domain = '') or (user = '') then
|
|---|
| 42 | exit; //invalid address format
|
|---|
| 43 | smtp:=tsmtpsend.create;
|
|---|
| 44 | mailservers:=tstringlist.create;
|
|---|
| 45 | dnsservers:=tstringlist.create;
|
|---|
| 46 | try
|
|---|
| 47 | dnsservers.CommaText := GetDNS;
|
|---|
| 48 | result := 3;
|
|---|
| 49 | if dnsservers.Count = 0 then
|
|---|
| 50 | Exit; // not DNS servers defined
|
|---|
| 51 | result := 2;
|
|---|
| 52 | b := false;
|
|---|
| 53 | for n := 0 to dnsservers.Count -1 do
|
|---|
| 54 | if GetMailServers(dnsservers[n], domain, mailservers) then
|
|---|
| 55 | begin
|
|---|
| 56 | b := true;
|
|---|
| 57 | break;
|
|---|
| 58 | end;
|
|---|
| 59 | if not b then
|
|---|
| 60 | Exit; // DNS cannot be contacted
|
|---|
| 61 | result := 5;
|
|---|
| 62 | if mailservers.Count = 0 then
|
|---|
| 63 | exit; // not defined MX record for requested domain
|
|---|
| 64 | b := false;
|
|---|
| 65 | for n := 0 to mailservers.count - 1 do
|
|---|
| 66 | begin
|
|---|
| 67 | smtp.TargetHost := mailservers[n];
|
|---|
| 68 | if not smtp.Login then
|
|---|
| 69 | Continue;
|
|---|
| 70 | b := true;
|
|---|
| 71 | if smtp.Verify(email) then
|
|---|
| 72 | begin
|
|---|
| 73 | if smtp.ResultCode < 252 then
|
|---|
| 74 | begin
|
|---|
| 75 | Result := 0; // user address confirmed!
|
|---|
| 76 | break;
|
|---|
| 77 | end;
|
|---|
| 78 | end
|
|---|
| 79 | else
|
|---|
| 80 | if smtp.ResultCode = 551 then
|
|---|
| 81 | begin
|
|---|
| 82 | Result := 6; // user address not confirmed!
|
|---|
| 83 | break;
|
|---|
| 84 | end;
|
|---|
| 85 | if not smtp.MailFrom('mailcheck@somewhere.com', 100) then
|
|---|
| 86 | Continue;
|
|---|
| 87 | if not smtp.MailTo(email) then
|
|---|
| 88 | begin
|
|---|
| 89 | Result := 6; // user address not confirmed!
|
|---|
| 90 | break;
|
|---|
| 91 | end
|
|---|
| 92 | else
|
|---|
| 93 | begin
|
|---|
| 94 | Result := 1; // address MAY exists
|
|---|
| 95 | break;
|
|---|
| 96 | end;
|
|---|
| 97 | end;
|
|---|
| 98 | if not b then
|
|---|
| 99 | result := 4; //cannot contact any mailserver;
|
|---|
| 100 | finally
|
|---|
| 101 | dnsservers.free;
|
|---|
| 102 | mailservers.free;
|
|---|
| 103 | smtp.free;
|
|---|
| 104 | end;
|
|---|
| 105 | end;
|
|---|
| 106 |
|
|---|
| 107 | end.
|
|---|