understanding an email server name

Jan 4, 2012 at 11:34am
I am creating a simple email sending program using winsock but I cant understand how am I suppose to get the email server's name. For example if I want to send an email to example@ex.com how can I understand the server's name they are using, or do I even need to ? I know I must connect to a server so I suppose you need to know the name of that server ;p

Thanks in advance and happy new year to everyone !
Jan 4, 2012 at 2:34pm
That's right. You need to ask for the server's name for outgoing mail (SMTP).

FYI, large enterprises (usually using Microsoft Exchange) publish all configuration settings in DNS, or at least that's what it seems to me because MS Outlook is able to discover any and all settings just by using the user's email address. I can only imagine that this information is being provided by the enterprise's DNS servers. How to get it? I don't know. Still, this information is not guaranteed to be in DNS so your application should be ready to ask the user for the server name.
Jan 4, 2012 at 3:54pm
If you are asking what I think you are asking, you need to look at the DnsQuery() function. You can use the domain-specific part of an email address and run an mx record search that will yield available mail server addresses for the specified domain. MSDN has a few entries on the subject.

http://msdn.microsoft.com/en-us/library/windows/desktop/ms682016(v=VS.85).aspx

http://support.microsoft.com/kb/831226
Jan 4, 2012 at 7:44pm
I was actually wondering how can I get the SMTP server of the email adress. Example :
I cant just connect to Hotmail.com with port 25 ( SMTP port ) because it has its own server, like some have mail.hotmail.com or smtp.hotmail.com but not everyone
Jan 5, 2012 at 6:32pm
so if you have the email address "someuser@comcast.net" you can do this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
#include <windns.h>

int main()
{
  PDNS_RECORD pdr = 0;
  DNS_STATUS result = DnsQuery("comcast.net",DNS_TYPE_MX,DNS_QUERY_STANDARD,0,&pdr,0);
  if(result)
  {
    std::cout << "DNS Lookup failed." << std::endl;
  } else {
    std::cout << "Mail Server for comcast.net is: " << pdr->Data.MX.pNameExchange << std::endl;
  }
  DnsRecordListFree(pdr,DnsFreeRecordList);
}


EDIT: Larger corporations and providers will have many mail servers so the DNS record returned may contain more than just the one mail server name. You can parse through the DNS_RECORD structure's pNext value and keep retrieving the mailserver name until pNext is NULL.
Last edited on Jan 5, 2012 at 6:34pm
Topic archived. No new replies allowed.