SMTP prolem

This is a discussion on SMTP prolem within the Windows Programming forums, part of the Platform Specific Boards category; Hi! I went through an emailing tutorial, and ended up with the code below. But it doesn’t work. I get ...

  1. #1
    Android
    Join Date
    Mar 2004
    Location
    Kurunegala Colony, Sri Lanka, Sri Lanka
    Posts
    374

    SMTP problem

    Hi!

    I went through an emailing tutorial, and ended up with the code below. But it doesn’t work. I get no compiler errors/warnings, nor runtime errors. Though it doesn’t send the email. Could someone please checkout the code and see what’s wrong?
    Thanks a lot.



    Code:
    	struct sockaddr_in A;WSADATA W;SOCKET S;struct hostent *H;
    	char aa[100];int i;char R[10000];
    	
    	WSAStartup (0x101, &W);
    	
    	S=socket(AF_INET, SOCK_STREAM,0);
    	
    	A.sin_family=AF_INET;
    	A.sin_port = htons(25); // SMPT is on port 25
    	H=gethostbyname("yahoo.com"); // get info on the email server
    	cout<<"gethostbyname()"<<GetLastError()<<endl;
    	A.sin_addr.s_addr=*((unsigned long *) H->h_addr); // convert the SMTP sites address into a IPA and store
    	i=connect(S,(struct sockaddr *) &A,sizeof(A)); // connect to the server
    	cout<<"connect()"<<GetLastError()<<endl;
    	
    	i=recv(S,R,10000,0); // wait until the connection establishes
    	cout<<"recv()[connect]"<<GetLastError()<<endl;
    	strset(aa,' ');
    	strcpy(R,"HELO P2V2E0\r\n");  // 1st command . computer name
    
    	i=send(S,R,strlen(R),0);
    	cout<<"send()"<<GetLastError()<<endl;
    	
    	i=recv(S,R,10000,0);
    	cout<<"recv()[send]"<<GetLastError()<<endl;
    	strset(aa,' ');
    	
    	strcpy(R,"MAIL FROM:<geekat02@yahoo.com>\r\n"); // our address
    	i=send(S,R,strlen(R),0);
    	
    	i=recv(S,R,10000,0);
    	strset(aa,' ');
    	
    	strcpy(R,"RCPT  TO:<geekat02@yahoo.com>\r\n"); // recepients' address
    	i=send(S,R,strlen(R),0);
    	
    	i=recv(S,R,10000,0);
    	strset(aa,' ');
    	
    	strcpy(R,"DATA\r\n"); // email begins
    	i=send(S,R,strlen(R),0);
    	
    	i=recv(S,R,10000,0);
    	strset(aa,' ');  
    	
    	strcpy(R,"TO: geekat02@yahoo.com\r\n");
    	//strcpy(R,"TO: aaa.com\r\n");
    	i=send(S,R,strlen(R),0);
    	
    	strcpy(R,"FROM: geekat02@yahoo.com\r\n");
    	i=send(S,R,strlen(R),0);
    	
    	strcpy(R,"DATE: 27 April 05 3:05 PST\r\n");
    	i=send(S,R,strlen(R),0);
    	
    	strcpy(R,"MESSAGE_ID: <geekat02@yahoo.com>\r\n");
    	i=send(S,R,strlen(R),0);
    	
    	strcpy(R,"Hello\r\n");
    	i=send(S,R,strlen(R),0);
    	
    	strcpy(R,"How are you\r\n");
    	i=send(S,R,strlen(R),0);
    	
    	strcpy(R,".\r\n"); // end the mail with a period
    	i=send(S,R,strlen(R),0);
    	
    	i=recv(S,R,10000,0);
    	strset(aa,' ');
    	
    	strcpy(R,"QUIT\r\n"); // end mail
    	i=send(S,R,strlen(R),0);
    	
    	i=recv(S,R,10000,0);
    Last edited by geek@02; 04-27-2005 at 03:49 AM.

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    329
    I don't think yahoo.com is a SMTP?
    try pop.mail.yahoo.com instead.

    From RFC821 You end the message by sending \r\n.\r\n

  3. #3
    Android
    Join Date
    Mar 2004
    Location
    Kurunegala Colony, Sri Lanka, Sri Lanka
    Posts
    374
    Thanks but it failed again.
    I Changed it to "pop.mail.yahoo.com" and ran the program. But it gives system errors 10060 (host failed to resond) at connect(), then 10057 (the socket is not connected) ?

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    329
    Try this instead:
    Code:
    struct sockaddr_in A;
    struct hostent *H;
    
    if((h = gethostbyname("yahoo.com")) == NULL)
      return;
    A.sin_addr = *(struct in_addr*)H->h_addr_list[0];
    A.sin_port = htons(usPort);
    A.sin_family = AF_INET;
    
    S=socket(AF_INET, SOCK_STREAM, 0);
    connect, etc.....
    Last edited by knutso; 04-28-2005 at 11:03 AM.

  5. #5
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    gethostbyname will return the A records for a domain. To find the mail server for a domain you must request the MX records. This will return zero or more mail server domains (you should use the one with the lowest preference). You can then use gethostbyname to retrieve the IP address for the mail server (this is often included in the original response as a matter of efficiency). Winsock does not provide MX lookup functionality. However, Windows 2000 and above include a DNS API. Alternatively, there is code on the web that implements a DNS client. For temporary testing, you can use a web page to look up the MX servers.

    Sample code that uses the Windows DNS API to query the MX records for yahoo.com:
    Code:
    #include <windows.h>
    #include <windns.h>
    #include <stdio.h>
    
    #if defined(_MSC_VER)
    #pragma comment(lib, "dnsapi.lib")
    #endif
    
    char* SectionToString(DWORD section)
    {
    	switch (section)
    	{
    		case DNSREC_QUESTION:   return "Question";
    		case DNSREC_ANSWER:     return "Answer";
    		case DNSREC_AUTHORITY:  return "Authority";
    		case DNSREC_ADDITIONAL: return "Additional";
    	}
    
    	return "Unknown";
    }
    
    int main(void)
    {
    	DNS_RECORD* pRecordList = NULL;
    	DNS_RECORD* pRecord     = NULL;
    
    	/* Send the DNS query... */
    	DnsQuery_A("yahoo.com", DNS_TYPE_MX, DNS_QUERY_STANDARD, NULL, &pRecordList, NULL);
    
    	/* Walk through the linked list of DNS records... */
    	for (pRecord = pRecordList; pRecord != NULL; pRecord = pRecord->pNext)
    	{
    		printf("\nSection:      %s\n", SectionToString(pRecord->Flags.S.Section));
    		printf(  "Domain Name:  %s\n", (char*) pRecord->pName);
    		printf(  "Record Type:  %d\n",         pRecord->wType);
    		printf(  "Time-To-Live: %d\n",         pRecord->dwTtl);
    
    		if (pRecord->wType == DNS_TYPE_MX)
    		{
    			printf("  MX Server:  %s\n", (char*) pRecord->Data.MX.pNameExchange);
    			printf("  Preference: %d\n",         pRecord->Data.MX.wPreference);
    		}
    	}
    
    	DnsRecordListFree(pRecordList, DnsFreeRecordList);
    
    	getchar();
    	return 0;
    }
    Output:
    Code:
    // Answer records contain the MX servers for yahoo.com
    Section:      Answer
    Domain Name:  yahoo.com
    Record Type:  15
    Time-To-Live: 770
      MX Server:  mx4.mail.yahoo.com
      Preference: 5
    
    Section:      Answer
    Domain Name:  yahoo.com
    Record Type:  15
    Time-To-Live: 770
      MX Server:  mx1.mail.yahoo.com
      Preference: 1
    
    Section:      Answer
    Domain Name:  yahoo.com
    Record Type:  15
    Time-To-Live: 770
      MX Server:  mx2.mail.yahoo.com
      Preference: 1
    
    Section:      Answer
    Domain Name:  yahoo.com
    Record Type:  15
    Time-To-Live: 770
      MX Server:  mx3.mail.yahoo.com
      Preference: 1
    
    // These additional records contain the IP addresses for
    // the MX servers. This saves an extra lookup.
    Section:      Additional
    Domain Name:  mx1.mail.yahoo.com
    Record Type:  1
    Time-To-Live: 770
    
    Section:      Additional
    Domain Name:  mx2.mail.yahoo.com
    Record Type:  1
    Time-To-Live: 770
    
    ...
    
    // These additional records contain the IP addresses for the yahoo
    // name server.
    Section:      Additional
    Domain Name:  ns1.yahoo.com
    Record Type:  1
    Time-To-Live: 770
    
    Section:      Additional
    Domain Name:  ns2.yahoo.com
    Record Type:  1
    Time-To-Live: 770
    
    ...
    Last edited by anonytmouse; 04-28-2005 at 12:04 PM. Reason: Changed messy while loop to cleaner for loop.

  6. #6
    Android
    Join Date
    Mar 2004
    Location
    Kurunegala Colony, Sri Lanka, Sri Lanka
    Posts
    374
    So, does it simply means that I can use "mx3.mail.yahoo.com" as yahoos' address, since it's the lowest in preference list?

  7. #7
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    >> So, does it simply means that I can use "mx3.mail.yahoo.com" as yahoo's address, since it's the lowest in preference list <<

    Yes, but only for the next 770 seconds as specified by the time-to-live value. After that, it may change (although in reality it probably only changes occasionally). If mx3 does not work you should try the other servers in order of preference.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SMTP with Apache/PHP on Windows
    By Mario F. in forum Tech Board
    Replies: 4
    Last Post: 02-29-2008, 08:24 AM
  2. SMTP host???
    By Yarin in forum Tech Board
    Replies: 6
    Last Post: 01-03-2008, 11:29 AM
  3. gmail SMTP
    By Scarvenger in forum Tech Board
    Replies: 6
    Last Post: 01-05-2007, 07:39 PM
  4. SMTP Server Not Working
    By (TNT) in forum Networking/Device Communication
    Replies: 1
    Last Post: 07-15-2003, 05:33 AM
  5. Win2k pro SMTP
    By _mails_ in forum Tech Board
    Replies: 1
    Last Post: 12-11-2002, 08:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21