'How can i resolve dns with TIdDNSResolver?

when try to resolve dns i am getting query refused by dns error error . am i wrong with that code can you please check ?

function Lookup(const Name: String): String;
var
  X: Integer;
  DNS: TIdDNSResolver;
begin
  DNS:= TIdDNSResolver.Create(nil);
  try
    DNS.Host := 'mail.relativ.art';
    DNS.WaitingTime := 5000;
    DNS.QueryType:= [qtMX];
    try DNS.Resolve(Name); except on E: Exception do begin writeln(E.Message); end; end;
    for X:= 0 to DNS.QueryResult.Count-1 do begin
      if DNS.QueryResult[X].RecType = qtMX then
        Result:= TMXRecord(DNS.QueryResult[X]).ExchangeServer;
    end;
  finally
    DNS.Free;
  end;

end;


Solution 1:[1]

A hostname with mail in its name, like mail.relativ.art, is likely not a DNS server.

You need to set the Host property to an actual DNS server (preferrably one provided by your ISP, or a public one like Google), and then pass the desired target name to Resolve().

If you are trying to query MX records for mail.relativ.art, then that is the target host to resolve, not the DNS server to send the query to, eg:

DNS:= TIdDNSResolver.Create(nil);
try
  DNS.Host := '8.8.8.8'; // Google public DNS
  DNS.WaitingTime := 5000;
  DNS.QueryType := [qtMX];
  try
    DNS.Resolve('mail.relativ.art');
  except
    on E: Exception do
    begin
      WriteLn(E.Message);
    end;
  end;
  for X:= 0 to DNS.QueryResult.Count-1 do
  begin
    if DNS.QueryResult[X].RecType = qtMX then
      Result := TMXRecord(DNS.QueryResult[X]).ExchangeServer;
  end;
finally
  DNS.Free;
end;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Remy Lebeau