BOOL CGetSMTPHostName::GetSmtpHostName(CString _EmailAddress, CString& _HostName)
{
BOOL bRV = TRUE;
_HostName.Empty();
int start = _EmailAddress.Find('@')+1;
CString strDomain = _EmailAddress.Mid(start,_EmailAddress.GetLength()-start);
if( ! strDomain.IsEmpty())
{
// First, look in our map to see if we have looked up this SMTP 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")
{
bRV=FALSE;
_HostName.Empty();
}
}
else
{
// 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;
m_SMTPHost.insert(HostMapValue(strDomain,_HostName));
}
else
{
// 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;
m_SMTPHost.insert(HostMapValue(strDomain,"UNK"));
}
}
}
else
{
// OOPS, is this a valid email address?
bRV = FALSE;
}
return(bRV);
}