Thread: solve this error

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    33

    solve this error

    Code:
    #include <stdio.h>
    #include <winsock.h>
    #include <stdlib.h>
    
    #pragma comment(lib, "wsock32.lib")
    
        #define waittime 1000
        #define bufsize 300
    
    
    int MailIt(char *mailserver, char *emailto, char *emailfrom,
               char *emailsubject, char *emailmessage) 
    {
    	SOCKET sockfd;
        WSADATA wsaData;
        FILE *smtpfile;
        
    
        int bytes_sent;   /* Sock FD */
        int err;
        struct hostent *host;   /* info from gethostbyname */
        struct sockaddr_in dest_addr;   /* Host Address */
        char line[1000];
        char *Rec_Buf = (char*) malloc(bufsize+1);
        smtpfile=fopen("SMTPLog.txt","a+");
        if (WSAStartup(0x202,&wsaData) == SOCKET_ERROR) 
    	{
    		fputs("WSAStartup failed",smtpfile);
    		WSACleanup();
    		return -1;
        }
        if ( (host=gethostbyname(mailserver)) == NULL) 
    	{
    		perror("gethostbyname");
    		exit(1);
        }
        memset(&dest_addr,0,sizeof(dest_addr));
        memcpy(&(dest_addr.sin_addr),host->h_addr,host->h_length);
    
    	/* Prepare dest_addr */
    	dest_addr.sin_family= host->h_addrtype;  /* AF_INET from gethostbyname */
    	dest_addr.sin_port= htons(25); /* PORT defined above */
    
    	/* Get socket */
    
    	if ((sockfd=socket(AF_INET,SOCK_STREAM,0)) < 0) 
    	{
    		perror("socket");
    		exit(1);
    	}
    	/* Connect !*/
    	fputs("Connecting....\n",smtpfile);
     
    	if (connect(sockfd, (struct sockaddr *)&dest_addr,sizeof(dest_addr)) == -1)
    	{
    		perror("connect");
            exit(1);
    	}
    	Sleep(waittime);
    	err=recv(sockfd,Rec_Buf,bufsize,0);Rec_Buf[err] = '\0';
    	fputs(Rec_Buf,smtpfile);
    	strcpy(line,"helo me.somepalace.com\n");
    	fputs(line,smtpfile);
    	bytes_sent=send(sockfd,line,strlen(line),0);
    	Sleep(waittime);
    	err=recv(sockfd,Rec_Buf,bufsize,0);Rec_Buf[err] = '\0';
    	fputs(Rec_Buf,smtpfile);
    	strcpy(line,"MAIL FROM:<");
    	strncat(line,emailfrom,strlen(emailfrom));
    	strncat(line,">\n",3);
    	fputs(line,smtpfile);
    	bytes_sent=send(sockfd,line,strlen(line),0);
    	Sleep(waittime);
    	err=recv(sockfd,Rec_Buf,bufsize,0);Rec_Buf[err] = '\0';
    	fputs(Rec_Buf,smtpfile);
    	strcpy(line,"RCPT TO:<");
    	strncat(line,emailto,strlen(emailto));
    	strncat(line,">\n",3);
    	fputs(line,smtpfile);
    	bytes_sent=send(sockfd,line,strlen(line),0);
    	Sleep(waittime);
    	err=recv(sockfd,Rec_Buf,bufsize,0);Rec_Buf[err] = '\0';
    	fputs(Rec_Buf,smtpfile);
    	strcpy(line,"DATA\n");
    	fputs(line,smtpfile);
    	bytes_sent=send(sockfd,line,strlen(line),0);
    	Sleep(waittime);
    	err=recv(sockfd,Rec_Buf,bufsize,0);Rec_Buf[err] = '\0';
    	fputs(Rec_Buf,smtpfile);
    	Sleep(waittime);
    	strcpy(line,"To:");
    	strcat(line,emailto);
    	strcat(line,"\n");
    	strcat(line,"From:");
    	strcat(line,emailfrom);
    	strcat(line,"\n");
    	strcat(line,"Subject:");
    	strcat(line,emailsubject);
    	strcat(line,"\n");
    	strcat(line,emailmessage);
    	strcat(line,"\r\n.\r\n");
    	fputs(line,smtpfile);
    	bytes_sent=send(sockfd,line,strlen(line),0);
    	Sleep(waittime);
    	err=recv(sockfd,Rec_Buf,bufsize,0);Rec_Buf[err] = '\0';
    	fputs(Rec_Buf,smtpfile);
    	strcpy(line,"quit\n");
    	fputs(line,smtpfile);
    	bytes_sent=send(sockfd,line,strlen(line),0);
    	Sleep(waittime);
    	err=recv(sockfd,Rec_Buf,bufsize,0);Rec_Buf[err] = '\0';
    	fputs(Rec_Buf,smtpfile);
    	fclose(smtpfile);                          
    	#ifdef WIN32
    	closesocket(sockfd);
    	WSACleanup();
    	#else
    	close(sockfd);
    	#endif
    }
    [Linker error] undefined reference to `WSAStartup@8'
    [Linker error] undefined reference to `WSACleanup@0'
    [Linker error] undefined reference to `gethostbyname@4'
    [Linker error] undefined reference to `htons@4'
    [Linker error] undefined reference to `send@16'
    [Linker error] undefined reference to `connect@12'

    and so more of same error

  2. #2
    Registered User
    Join Date
    Jul 2006
    Posts
    162
    maybe '-lws2_32' ? (dev-cpp). w/ #include <WinSock2.h>

    uhm, what ever the equivalent is for winsock (1?) you need to link that dll/library.
    Last edited by simpleid; 05-27-2008 at 03:36 PM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Awesome...the most obvious source of this function seems to be a keylogger. Sigh.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM