Click here to Skip to main content
13,353,706 members (62,343 online)
Click here to Skip to main content
Add your own
alternative version


35 bookmarked
Posted 27 Feb 2003

Getting an MX record (the easy way)

, 27 Feb 2003
Rate this:
Please Sign up or sign in to vote.
Using DNSQuery() to get an MX record
<!-- Add the rest of your HTML here -->


A few months ago I was working on an application that would send emails to users that had subscribed to a newsletter. I know that there are numerous mass mail applications but I really wanted to develop an in-house solution. What I present in this article is a set of classes that are used by the system to validate email addresses.

Credit where it's due

As has come to be my modus operandi in my articles, I have to give thanks to James Oldroyd for helping me find the ::DnsQuery() function. James is an amazing coder and if you can get him to stop thinking in code for more than five minutes you can even have a conversation with him. Before using ::DNSQuery() I had examined some C code that connected to the DNS server and did an MX record request – a real nasty mess of code to say the least. So finding and then using the ::DNSQuery() function was a no-brainer.


The demo application is a Windows Console Application that accepts email addresses interactively. Entering an email address and pressing the RETURN key starts the email address validation process. Typing exit and pressing the RETURN key will exit the application. The simple demo makes use of two classes that I am describing in this article, CGetSMTPHostName which makes use of ::DnsQuery() and CVerifySMTPEmailAddress which does the actual email validation. It should be noted that I decided to split the classes up into two distinct classes because of the way that I intend to use them but they could be combined into a single class. Below I give a brief description of each.


The CGetSMTPHostName class provides three member functions: GetSmtpHostName(), ResetHostMap() and Dump(). It also contains a std::map that holds the DomainName and SMTPHostName pair.


This method accepts an email address and a CString& to return the host name for the email address. The host name is found using the ::DNSQuery() function. The function starts by stripping the Domain part of the email address and looking in our std::map to determine if we have previously performed a lookup on this domain. If so, we return the host name and we are done. If not, then we make a call to ::DNSQuery() to determine the hostname. If the call is successful we insert the domain and hostname pair into the std::map and return. Here's the code:
BOOL CGetSMTPHostName::GetSmtpHostName(CString _EmailAddress, <BR>                                       CString& _HostName)
    BOOL bRV = TRUE;
    int start = _EmailAddress.Find('@')+1;
    CString strDomain<BR>                   = _EmailAddress.Mid(start,_EmailAddress.GetLength()-start);
    if( ! strDomain.IsEmpty())
         // First, look in our map to see if we have looked up this SMTP <BR>         // Host before
         m_SMTPHostIterator = m_SMTPHost.find(strDomain);
         if(m_SMTPHostIterator != m_SMTPHost.end())
             // Great, we've looked this one up before...
             _HostName = (*m_SMTPHostIterator).second;
         // Is it unknown???
         if(_HostName == "UNK")
         // OK, we haven't looked this up before, so look it up
         DNS_RECORD* ppQueryResultsSet = NULL;
         DNS_STATUS statusDNS = ::DnsQuery( strDomain, DNS_TYPE_MX, 
                                                DNS_QUERY_STANDARD, NULL, 
                                                &ppQueryResultsSet, NULL );
             if(statusDNS == ERROR_SUCCESS)
             // Found the SMTP Host Name, insert it into our map
        _HostName = ppQueryResultsSet->Data.MX.pNameExchange;
        // I have opted to place unknown domains in the map to reduce
        // the time that I spend looking up the domain names.  You
        // may want to implement this differently for your usage.
        bRV = FALSE;
        DNS_STATUS theError = statusDNS;
    // OOPS, is this a valid email address?
    bRV = FALSE;


This method is nothing more than a utility function to clear the std::map in the event that you need to start fresh.


Another utility function to print the the console the list of domains and hostnames that are contained within the std::map.


This class contains (has a) CGetSMTPHostName class as a private member and had a single public method called VerifyEmailAddress() shown below:
BOOL CVerifySMTPEmailAddress::VerifyEmailAddress(CString _EmailAddress)
    CString strHostName;
    if(m_GetSmtpHostName.GetSmtpHostName(_EmailAddress, strHostName))    
    return(Connect(strHostName, _EmailAddress));

main() or _tmain()

Here's the main function of the console application:
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
    int nRetCode = 0;

    // initialize MFC and print and error on failure
    if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
        // TODO: change error code to suit your needs
        cerr << _T("Fatal Error: MFC initialization failed") << endl;
        nRetCode = 1;
        CVerifySMTPEmailAddress vrfyemail;
    CString strSMTPAddress;
    CString strHostName;

    char    caSMTPAddress[128];
            std::cout << "Enter SMTP Address : ";
        std::cin >> caSMTPAddress;
        strSMTPAddress = caSMTPAddress;
        if(strSMTPAddress == "exit")
            std::cout << strSMTPAddress.GetBuffer(0) << " is VALID" <BR>                      << std::endl;
        std::cout << strSMTPAddress.GetBuffer(0) << vrfyemail.GetLastError() <BR>                  << std::endl;
    return nRetCode;


::DNSQuery()[^] can be used for much more than MX Record lookups. Information can be found on MSDN (click the link).

I prefer to use #pragma comment to link in libraries, especially on demonstration applications such as this. You can set up the link libraries to suit your taste.


I wrote this article for one purpose and that was to show that the libraries provided by Microsoft are bountiful. So before you start writing a DNS MX Record lookup function or the like take a look in MSDN or ask a living MSDN index (a.k.a. James) if an existing library contains the functionality that you need.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Dave Loeser
Web Developer
United States United States
Dave has been programming for the past 20+ years first on a variety of platforms and operating systems using various languages. As a hobbyist Dave cut his teeth on the Commodore Pet and the 64 coding in basic and then moving to 6502 ASM. Dave moved to the Amiga using 68000 ASM and then C. His knowledge of the C language offered the stepping stone for him to make his hobby his profession taking a position coding C on an AIX Unix platform. Since then he has worked on many flavors of Unix, QNX, Windows (3.11 – present), and has been coding games for his Pocket PC in his spare time.

Dave lives in Indiana with his two teenage daughters and two cats.

You may also be interested in...


Comments and Discussions

Questionm_SMTPServer.Connect(_HostName... Is failing every time Pin
Member 471758915-Jan-17 21:29
memberMember 471758915-Jan-17 21:29 
Generalnice job Pin
skumria5-Sep-06 9:58
memberskumria5-Sep-06 9:58 
QuestionDnsQuery API timeout? Pin
el1215-Aug-05 0:46
sussel1215-Aug-05 0:46 
GeneralDnsQuery ErrorCode 9002 Pin
_happymozart_11-May-05 10:06
member_happymozart_11-May-05 10:06 
GeneralDnsQuery() Is a very dependent function Pin
G_S14-Feb-05 14:37
memberG_S14-Feb-05 14:37 
GeneralDNS adding/modifying zone records Pin
kumatbp10-Oct-04 18:24
memberkumatbp10-Oct-04 18:24 
GeneralDNS adding/modifying zone records Pin
kumatbp10-Oct-04 18:24
memberkumatbp10-Oct-04 18:24 
Generalthanks, before i use udp and tcp to query it Pin
SevenCat25-Oct-03 0:17
memberSevenCat25-Oct-03 0:17 
Generalwrite some code, compare the Preference(rfc requir this)) Pin
SevenCat27-Oct-03 19:33
memberSevenCat27-Oct-03 19:33 
GeneralDNSQuery not available on Win9x Pin
Marc G4-Sep-03 5:05
memberMarc G4-Sep-03 5:05 
Questionhow to get more than one mx records? Pin
bborn12-Aug-03 21:18
memberbborn12-Aug-03 21:18 
AnswerRe: how to get more than one mx records? Pin
Paul Ingles25-Oct-03 1:36
memberPaul Ingles25-Oct-03 1:36 
GeneralJust being picky ;-) Pin
pierrec16-Jul-03 6:04
memberpierrec16-Jul-03 6:04 
QuestionCan i use to query ? Pin
ScorpioMidget21-May-03 6:52
memberScorpioMidget21-May-03 6:52 
GeneralLinker error Pin
Anthony_Yio2-Mar-03 17:58
memberAnthony_Yio2-Mar-03 17:58 
GeneralRe: Linker error Pin
Anthony_Yio2-Mar-03 18:04
memberAnthony_Yio2-Mar-03 18:04 
Generalspam Pin
zoldat2-Mar-03 16:40
memberzoldat2-Mar-03 16:40 
GeneralRe: spam Pin
jeoshah19-Jun-03 2:13
memberjeoshah19-Jun-03 2:13 
GeneralGood attempt, but.... Pin
hector santos1-Mar-03 3:16
memberhector santos1-Mar-03 3:16 
GeneralRe: Good attempt, but.... Display Correction Pin
hector santos1-Mar-03 3:20
memberhector santos1-Mar-03 3:20 
GeneralRe: Good attempt, but.... Display Correction Pin
Gisle Vanem1-Mar-03 3:45
memberGisle Vanem1-Mar-03 3:45 
GeneralRe: Good attempt, but.... Display Correction Pin
hector santos1-Mar-03 4:12
memberhector santos1-Mar-03 4:12 
GeneralRe: Good attempt, but.... Display Correction Pin
shankargiri8-Feb-11 8:55
membershankargiri8-Feb-11 8:55 
GeneralRe: Good attempt, but.... Pin
Dave Loeser1-Mar-03 5:38
memberDave Loeser1-Mar-03 5:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180111.1 | Last Updated 28 Feb 2003
Article Copyright 2003 by Dave Loeser
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid