Re: DNS MX Record Issue

Giganews Newsgroups
Subject: Re: DNS MX Record Issue
Posted by:  Remy Lebeau (TeamB) (no.spam@no.spam.com)
Date: Wed, 19 Oct 2005

"Jesse" <nospam@fake.com> wrote in message
news:E4220CC091DEE240nospam@fake.com...

> I have been using the following function with Indy 9 to retrieve MX Server
> names to send e-mails to.  It worked just fine.

Almost fine.  There is a memory leak in that code when you call the function
recursively.  Every time you call the function, you allocate a new
TStringList.  The final TStringList is returned to the calling code, but you
don't free any of the previous TStringList instances.

Also, you should take into account that multiple TNSRecord items can be an a
query result.  If one server fails, you should try the next server in the
list.

For example (untested):

    function TMainFrm.GetMXServers(const cNSServer: String; const cDomain:
String; MXServers: TStrings): Boolean;
    var
        i : Integer;
        NSServers: TStringList;
    begin
        Result := False;
        if cNSServer = '' then Exit;

        DNSResolver.Host := cNSServer;
        DNSResolver.Resolve(cDomain);

        for i := 0 to DNSResolver.QueryResult.Count-1 do
        begin
            if DNSResolver.QueryResult[i] is TMXRecord then
            begin
                with TMXRecord(DNSResolver.QueryResult[i]) do

MXServers.AddObject(ExchangeServer,TObject(Integer(Preference)));
            end;
        end;

        if MXServers.Count > 0 then
        begin
            MXServers.CustomSort(SortServers);
            Result := True;
            Exit;
        end;

        NSServers := nil;
        try
            for i := 0 to DNSResolver.QueryResult.Count-1 do
            begin
                if DNSResolver.QueryResult[i] is TNSRecord then
                begin
                    with TNSRecord(DNSResolver.QueryResult[i]) do
                    begin
                        if HostName <> '' then begin
                        end;
                            if NSServers = nil then NSServers :=
TStringList.Create;
                            NSServers.Add(HostName);
                        end;
                    end;
                end;
            end;
            if NSServers <> nil then
            begin
                for i := 0 to NSServers.Count-1 do
                begin
                    Result := GetMXServers(NSServers[i], cDomain,
MXServers);
                    if Result then Exit;
                end;
            end;
        finally
            if NSServers <> nil then NSServers.Free;
        end;
    end;

> I upgraded to Indy 10 to solve a problem I was having with TidMessage,
> and now, this function always returns "mail.msdlg.com".  mail.msdlg.com
> is our mail server.

What DNS server(s) were you redirected to?  Which exact result items were
returned at each server?

For example, you can use the following URL to see an example of an MX lookup
for hotmail.com:

  http://www.dnsstuff.com/tools/lookup.ch?name=hotmail.com&type=MX

Gambit

Replies

In response to

DNS MX Record Issue posted by Jesse on Wed, 19 Oct 2005