Send email

This is a discussion on Send email within the Windows Programming forums, part of the Platform Specific Boards category; Hi, I need to send an email from my program. How can I do this? I found this fucnction, but ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    230

    Send email

    Hi,
    I need to send an email from my program. How can I do this? I found this fucnction, but it gives me errors at compile time:
    Code:
    int MailIt (char *mailserver, char *emailto, char *emailfrom, 
    			char *emailsubject, char *emailmessage) 
    {
    	SOCKET sockfd;
        WSADATA wsaData;
        FILE *smtpfile;
        
        #define bufsize 300
        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,"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
    }
    If someone can help me fix this out I'd be very thankful, the problem is I don't know much about Windows API, I can only use ready functions.

  2. #2
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,597
    Fixed.
    Code:
    int MailIt(char *mailserver, char *emailto, char *emailfrom,
               char *emailsubject, char *emailmessage) 
    {
    	SOCKET sockfd;
        WSADATA wsaData;
        FILE *smtpfile;
        
        #define waittime 1000
        #define bufsize 300
        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
    }
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    Thank you but I still don't know what files I need to include. When I include winsock.h it gives me some kind of long warning that there might be an error at runtime.
    And if I'm using Gmail, what mailserver do I use?

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    OK I'm assuming the warning above isn't a problem. I tried smtp.gmail.com as a server but the message wasn't sent. Here's the log:
    Code:
    Connecting....
    220 mx.google.com ESMTP y37sm15892923iky.7
    helo me.somepalace.com
    250 mx.google.com at your service
    MAIL FROM:<from@gmail.com>
    530 5.7.0 Must issue a STARTTLS command first y37sm15892923iky.7
    RCPT TO:<to@gmail.com>
    530 5.7.0 Must issue a STARTTLS command first y37sm15892923iky.7
    DATA
    530 5.7.0 Must issue a STARTTLS command first y37sm15892923iky.7
    To:to@gmail.com
    From:from@gmail.com
    Subject:
    This is a test
    .
    502 5.5.1 Unrecognized command y37sm15892923iky.7
    502 5.5.1 Unrecognized command y37sm15892923iky.7
    502 5.5.1 Unrecognized command y37sm15892923iky.7
    502 5.5.1 Unrecognized command y37sm15892923iky.7
    quit
    502 5.5.1 Unrecognized command y37sm15892923iky.7
    554 5.7.0 Too Many Unauthenticated commands. y37sm15892923iky.7
    NOTE: I changed the emails for privacy

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,437
    Goto http://www.rfc-editor.org
    Search for SMTP (RFC821 for example)
    Do far more with the responses than simply dumping them in a file.

    Most mail relays require you to authenticate in some way. Those that aren't are loved by spammers, and so are either very busy or very soon on everyone's blacklists.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to send email using c
    By moussa in forum C++ Programming
    Replies: 7
    Last Post: 05-28-2008, 08:19 PM
  2. Spam Filters. ISP based email is dying.
    By Mario F. in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 03-05-2008, 11:05 AM
  3. Send email via c++
    By davidboja in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2007, 07:51 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  5. How to Send Email
    By sampatel in forum C Programming
    Replies: 1
    Last Post: 11-23-2002, 01:07 PM

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