How to discover Mail Servers for a particular Domain

In this example, gmail.com will server as a good example due to the complexity of the output.

The Inputs - what is required to lookup a mail server?
  • The domain - in this case 'gmail.com'
  • The Record Type, in this case use NsType.MX to specify MailExchange

The Outputs - what will the DnDns object model return?
  • The Answers - the DnsQueryResponse.Answers[] array will contain all of the MX Records gmail has in their DNS
    • What's important to realize is that this will return every Mail Exchange DOMAIN NAME (not an IP address) that gmail has as a Mail Exchange (MX)
  • The AdditionalRecords - these are going to contain the IP Address of corresponding MX records
    • To correlate which IP's go with which MX server name, compare the additionalRecord.Name property to the MxRecord.MailExchange property.
    • Notice the DNS server won't necessarily return an ARecord in the DnsQueryResponse.AdditionalRecords[] array for every MxRecord returned from the server.

The Code
string dnsServer = "192.168.0.1";
string domainToResolve = "gmail.com";

DnsQueryRequest request = new DnsQueryRequest();
DnsQueryResponse response = request.Resolve(dnsServer, domainToResolve, NsType.MX, NsClass.INET, ProtocolType.Udp);

Console.WriteLine("Bytes received: " + response.BytesReceived);

// Enumerate the Answer Records
Console.WriteLine("Answers:");
foreach (IDnsRecord record in response.Answers)
{
    Console.WriteLine(record.Answer);
    // Common IDnsRecord Fields
    Console.WriteLine("  |--- RDATA Field Length: " + record.DnsHeader.DataLength);
    Console.WriteLine("  |--- Name: " + record.DnsHeader.Name);
    Console.WriteLine("  |--- NS Class: " + record.DnsHeader.NsClass);
    Console.WriteLine("  |--- NS Type: " + record.DnsHeader.NsType);
    Console.WriteLine("  |--- TTL: " + record.DnsHeader.TimeToLive);

    if (record is MxRecord)
    {
        // MxRecord specific fields
        MxRecord mailRecord = (MxRecord)record;
        Console.WriteLine("  |--- Mail Server: " + mailRecord.MailExchange);
        Console.WriteLine("  |--- Preference: " + mailRecord.Preference);
    }
    
    Console.WriteLine();
}

Console.WriteLine("Additional Records:");
foreach (IDnsRecord additionalRecord in response.AdditionalRecords)
{
    Console.WriteLine(additionalRecord.Answer);
    Console.WriteLine("  |--- RDATA Field Length: " + additionalRecord.DnsHeader.DataLength);
    Console.WriteLine("  |--- Name: " + additionalRecord.DnsHeader.Name);
    Console.WriteLine("  |--- NS Class: " + additionalRecord.DnsHeader.NsClass);
    Console.WriteLine("  |--- NS Type: " + additionalRecord.DnsHeader.NsType);
    Console.WriteLine("  |--- TTL: " + additionalRecord.DnsHeader.TimeToLive);
    Console.WriteLine();
}


Output
Bytes received: 222
Answers:
MX Preference: 10, Mail Exchanger: alt1.gmail-smtp-in.l.google.com.
  |--- RDATA Field Length: 32
  |--- Name: gmail.com.
  |--- NS Class: INET
  |--- NS Type: MX
  |--- TTL: 2191
  |--- Mail Server: alt1.gmail-smtp-in.l.google.com.
  |--- Preference: 10

MX Preference: 10, Mail Exchanger: alt2.gmail-smtp-in.l.google.com.
  |--- RDATA Field Length: 9
  |--- Name: gmail.com.
  |--- NS Class: INET
  |--- NS Type: MX
  |--- TTL: 2191
  |--- Mail Server: alt2.gmail-smtp-in.l.google.com.
  |--- Preference: 10

MX Preference: 50, Mail Exchanger: gsmtp163.google.com.
  |--- RDATA Field Length: 13
  |--- Name: gmail.com.
  |--- NS Class: INET
  |--- NS Type: MX
  |--- TTL: 2191
  |--- Mail Server: gsmtp163.google.com.
  |--- Preference: 50

MX Preference: 50, Mail Exchanger: gsmtp183.google.com.
  |--- RDATA Field Length: 13
  |--- Name: gmail.com.
  |--- NS Class: INET
  |--- NS Type: MX
  |--- TTL: 2191
  |--- Mail Server: gsmtp183.google.com.
  |--- Preference: 50

MX Preference: 5, Mail Exchanger: gmail-smtp-in.l.google.com.
  |--- RDATA Field Length: 4
  |--- Name: gmail.com.
  |--- NS Class: INET
  |--- NS Type: MX
  |--- TTL: 2191
  |--- Mail Server: gmail-smtp-in.l.google.com.
  |--- Preference: 5

Additional Records:
Address: 209.85.129.27
  |--- RDATA Field Length: 4
  |--- Name: alt1.gmail-smtp-in.l.google.com.
  |--- NS Class: INET
  |--- NS Type: A
  |--- TTL: 300

Address: 64.233.163.27
  |--- RDATA Field Length: 4
  |--- Name: gsmtp163.google.com.
  |--- NS Class: INET
  |--- NS Type: A
  |--- TTL: 5791

Address: 64.233.183.27
  |--- RDATA Field Length: 4
  |--- Name: gsmtp183.google.com.
  |--- NS Class: INET
  |--- NS Type: A
  |--- TTL: 5835

Address: 72.14.205.27
  |--- RDATA Field Length: 4
  |--- Name: gmail-smtp-in.l.google.com.
  |--- NS Class: INET
  |--- NS Type: A
  |--- TTL: 199

Last edited Feb 5, 2008 at 2:44 PM by jmonty, version 1

Comments

No comments yet.