user8584489
user8584489

Reputation:

How programmatically find correct smtp server address based only on email address domain?

I have a smtp scanner (brute force multithread program that tests each email account using a password list previous defined). The program is the : Sanmao SMTP Mail Cracker.

Files of program

One thing that called my attention in this program was the capacity of assign of smtp server address to each email.

All know that normally a smtp server address start with the words: mail or smtp followed of domain.

Examples:

until this moment all fine.

But i saw a email with following domain: [email protected] and the program assigned eu-smtp-inbound-2.mimecast.com as smtp server address.

Now i wants know how is possible assign correctly ("discover") a smtp address to any email address (like was made in this program) programatically in Delphi.

Thank you.

Update:

After answer of @Remy Lebeau i have this code based in a other answer.

function ResolveMx(email: string; dnsHost: string): string;
var
  DNS: TIdDNSResolver;
  I, J: Integer;
  sDomain: string;
  Record_: TResultRecord;
  Txt: TTextRecord;
  Srv: TSRVRecord;
  MX: TMXRecord;
begin
  DNS := TIdDNSResolver.Create(nil);
  try
    J := Pos('@', email);
    if (J > 0) then
      sDomain := Copy(email, Succ(J), Length(email))
    else
      sDomain := email;
    DNS.WaitingTime := 3000;
    DNS.QueryType := [qtTXT, qtService, qtMX];
    DNS.Host := dnsHost;

    try

      DNS.Resolve(sDomain);
    except
      on e: exception do
        Form1.mmo1.Lines.Add(e.message);
    end;

    for I := 0 to DNS.QueryResult.Count - 1 do
    begin
      Record_ := DNS.QueryResult[I];
      case Record_.RecType of
        qtTXT:
          begin
            Txt := TTextRecord(Record_);
            // use Txt.Text as needed...
          end;
        qtService:
          begin
            Srv := TSRVRecord(Record_);
            // use Srv.OriginalName, Srv.Service, Srv.Protocol, etc as needed...
          end;
        qtMX:
          begin
            MX := TMXRecord(Record_);
            Result := MX.ExchangeServer;
          end
      else
        // something else...
      end;
    end;
  finally
    DNS.Free;
  end;
end;

But nothing is returned.

How solve?

Update2:

The code is working correctly after insert a well-known dns server (8.8.8.8, of Google) like suggested by @Remy Lebeau.

Upvotes: 0

Views: 735

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 596377

You need to perform a DNS query to lookup a given domain's MX record. That will tell you the address(es) of its SMTP server(s).

For instance, you can use Indy's TIdDNSResolver component for that purpose.

Upvotes: 2

Related Questions