Sample code for 30+ languages & platforms
Unicode C

MX Lookup Mail Server Domain by Email Address

How to find the mail server for a given email address. Returns the domain name of the primary mail server.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkDnsW.h>
#include <C_CkJsonObjectW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkDnsW dns;
    HCkJsonObjectW json;
    const wchar_t *emailAddr;
    int i;
    int count_i;
    const wchar_t *domain;

    success = FALSE;

    //  This example requires the Chilkat API to have been previously unlocked.
    //  See Global Unlock Sample for sample code.

    dns = CkDnsW_Create();
    json = CkJsonObjectW_Create();

    emailAddr = L"bob@example.com";

    //  This gets all MX domains for an email address.  (Typically one domain.)
    //  The preferred domain will be at index 0 (see below).
    success = CkDnsW_Query(dns,L"MX",emailAddr,json);
    if (success == FALSE) {
        wprintf(L"%s\n",CkDnsW_lastErrorText(dns));
        CkDnsW_Dispose(dns);
        CkJsonObjectW_Dispose(json);
        return;
    }

    i = 0;
    count_i = CkJsonObjectW_SizeOfArray(json,L"answer.mx");
    while (i < count_i) {
        CkJsonObjectW_putI(json,i);
        domain = CkJsonObjectW_stringOf(json,L"answer.mx[i].domain");
        wprintf(L"%s\n",domain);
        i = i + 1;
    }



    CkDnsW_Dispose(dns);
    CkJsonObjectW_Dispose(json);

    }