Thread: e-mail with c/c++

  1. #16
    Registered User deltabird's Avatar
    Join Date
    Jan 2003
    Posts
    73
    any ideas? anyone have a working example i could look at?

  2. #17
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Read Salem's post again.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #18
    Registered User deltabird's Avatar
    Join Date
    Jan 2003
    Posts
    73
    The two new lines didn't make a difference. What it does is the program stops after writing "data". It seems like it's waiting for input. The program doesn't complete.

    Here's what I mean: http://leeman_s.tripod.com/mail.JPG
    Last edited by deltabird; 06-28-2003 at 06:14 PM.

  4. #19
    Registered User deltabird's Avatar
    Join Date
    Jan 2003
    Posts
    73
    Code:
    #include <stdio.h>
    #include <io.h>\
    
    int  rc;
        char buf[256];
    
    //#define LINUX /* define this if you are on linux   */
    //#define WIN32 /* define this if you are on windows */
    
    #ifdef WIN32
    #       include "io.h"
    #       include "winsock2.h"      /* WSAGetLastError, WSAStartUp  */
    #       define snprintf _snprintf
    #endif
    
    #ifdef LINUX
    #       include <netdb.h>         /* gethostbyname  */
    #       include <netinet/in.h>    /* htons          */
    #       include <sys/socket.h>
    #endif
    
    #pragma comment(lib, "wsock32.lib")
    
    static void sendmail_write(const int  sock,const char *str,const char *arg) 
    {
        char buf[4096];
    
        if (arg != NULL)
            snprintf(buf, sizeof(buf), str, arg);        
        else
            snprintf(buf, sizeof(buf), str);
        
        send(sock, buf, strlen(buf), 0); 
    	
    	if((rc = recv(sock, buf, sizeof(buf) - 1, 0)) == -1) 
    	{
    		perror("recv");
    		printf("%s", "ERROR");
    	} 
    	else 
    	{
    		buf[rc] = 0;
    		printf("%s", buf);
    	}
    }
    
    static int sendmail(const char *from,const char *to,const char *subject,const char *body, const char *hostname,const int   port)
    {
        struct hostent *host;   
        struct sockaddr_in saddr_in;
        int sock = 0;
    
    #ifdef WIN32
    	WSADATA wsaData;
    	if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) 
    	{
    		return -1;
    	}
    #endif
    
        sock = socket(AF_INET, SOCK_STREAM, 0);
    	host = gethostbyname(hostname);
    	
    	saddr_in.sin_family      = AF_INET;
    	saddr_in.sin_port        = htons((u_short)port);
    	saddr_in.sin_addr.s_addr = 0;
    
    	memcpy((char*)&(saddr_in.sin_addr), host->h_addr, host->h_length);
    
    	if(connect(sock, (struct sockaddr*)&saddr_in, sizeof(saddr_in)) == -1)
    	{
    		return -2;
    	}
        
        sendmail_write(sock, "helo %s\r\n",       from);    // greeting
        sendmail_write(sock, "mail from: %s\r\n", from);    // from
        sendmail_write(sock, "rcpt to: %s\r\n",   to);      // to
        sendmail_write(sock, "data\r\n",        NULL);    // begin data
    	//printf("%s", "\nWe are after quit command");
    
        // next comes mail headers
        sendmail_write(sock, "From: %s\r\n",      from);
        sendmail_write(sock, "To: %s\r\n",        to);
        sendmail_write(sock, "Subject: %s\r\n",   subject);
    	sendmail_write(sock, "Date: 6/6/6\r\n", NULL);
    
        sendmail_write(sock, "\r\n",              NULL);
    
        sendmail_write(sock, "%s\r\n",            body);    // data
    
        sendmail_write(sock, ".\r\n",             NULL);    // end data
    	//Sleep(5000);
        sendmail_write(sock, "\r\n.\r\n",         NULL);    // terminate
    	printf("%s", "\nWe are after quit command"); //never even gets to this line
    
        #ifdef WIN32
        closesocket(sock);
        #else
        close(sock);
        #endif
    
        return 0;
    }
    
    int main(int argc, char *argv[]) {
    
    	
        
        int ret = sendmail(
            "[email protected]",   /* from     */
            "[email protected]",       /* to       */
            "Subject",              /* subject  */
            "body",                 /* body     */
            "mx2.hotmail.com",             /* hostname */
            25                      /* port     */
        );
    
        if (ret != 0)
            fprintf(stderr, "Failed to send mail (code: %i).\n", ret);
        else
            fprintf(stdout, "Mail successfully sent.\n");
    
        return ret;
    }
    see the line that says printf("%s", "\nWe are after quit command"); , the program never even gets to that line. It sort of stops mid-program and hangs there. If I take out the bit that says:
    Code:
    if((rc = recv(sock, buf, sizeof(buf) - 1, 0)) == -1) 
    	{
    		perror("recv");
    		printf("%s", "ERROR");
    	} 
    	else 
    	{
    		buf[rc] = 0;
    		printf("%s", buf);
    	}
    ....then it actually finishes, but the mail doesn't get sent still. But the program at least finishes (gets to the end). Any ideas? I mean, somebody HAS to have at least one working example right?

  5. #20
    id id
    Guest
    I think the problem is that you are sending <crlf>.<crlf> twice. Try replacing the second instance with QUIT

    Add a printf so you see what you are sending out.

    If you are still having problems post the program output.

    Also try these links:

    http://ostrosoft.com/smtp_component/smtp_demo.asp

    http://www.codeproject.com/useritems/smtp.asp

    http://www.stormpages.com/bojanjurca/CSamples.htm

    http://www.google.com/search?q=smtp+...+source+code+c

    Good Luck!
    Have Fun!

  6. #21
    id id
    Guest
    PS!!!!


    You do not appear to be reading out the server welcome message. Do a recv call to remove this from the input buffer before sending anything.

  7. #22
    Registered User deltabird's Avatar
    Join Date
    Jan 2003
    Posts
    73
    still doesn't work. I tried one of thsoe examples and it had the same problem as my program. can somebody try running this code on their machine and tell me if it works or not? tell me what it does please.

    here is the pic of what it does on mine: http://leeman_s.tripod.com/mail.JPG

    Code:
    #include <stdio.h>
    #include <io.h>
    
    int  rc;
        char buf[256];
    
    //#define LINUX /* define this if you are on linux   */
    //#define WIN32 /* define this if you are on windows */
    
    #ifdef WIN32
    #       include "io.h"
    #       include "winsock2.h"      /* WSAGetLastError, WSAStartUp  */
    #       define snprintf _snprintf
    #endif
    
    #ifdef LINUX
    #       include <netdb.h>         /* gethostbyname  */
    #       include <netinet/in.h>    /* htons          */
    #       include <sys/socket.h>
    #endif
    
    #pragma comment(lib, "wsock32.lib")
    
    static void sendmail_write(const int  sock,const char *str,const char *arg) 
    {
        char buf[4096];
    
        if (arg != NULL)
            snprintf(buf, sizeof(buf), str, arg);        
        else
            snprintf(buf, sizeof(buf), str);
        
        send(sock, buf, strlen(buf), 0); 
    	
    	if((rc = recv(sock, buf, sizeof(buf) - 1, 0)) == -1) 
    	{
    		perror("recv");
    		printf("%s", "ERROR");
    	} 
    	else 
    	{
    		buf[rc] = 0;
    		printf("%s", buf);
    	}
    }
    
    static int sendmail(const char *from,const char *to,const char *subject,const char *body, const char *hostname,const int   port)
    {
        struct hostent *host;   
        struct sockaddr_in saddr_in;
        int sock = 0;
    
    #ifdef WIN32
    	WSADATA wsaData;
    	if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) 
    	{
    		return -1;
    	}
    #endif
    
        sock = socket(AF_INET, SOCK_STREAM, 0);
    	host = gethostbyname(hostname);
    	
    	saddr_in.sin_family      = AF_INET;
    	saddr_in.sin_port        = htons((u_short)port);
    	saddr_in.sin_addr.s_addr = 0;
    
    	memcpy((char*)&(saddr_in.sin_addr), host->h_addr, host->h_length);
    
    	if(connect(sock, (struct sockaddr*)&saddr_in, sizeof(saddr_in)) == -1)
    	{
    		return -2;
    	}
        
        sendmail_write(sock, "helo %s\r\n",       from);    // greeting
        sendmail_write(sock, "mail from: %s\r\n", from);    // from
        sendmail_write(sock, "rcpt to: %s\r\n",   to);      // to
        sendmail_write(sock, "data\r\n",        NULL);    // begin data
    	//printf("%s", "\nWe are after quit command");
    
        // next comes mail headers
        sendmail_write(sock, "From: %s\r\n",      from);
        sendmail_write(sock, "To: %s\r\n",        to);
        sendmail_write(sock, "Subject: %s\r\n",   subject);
    	sendmail_write(sock, "Date: 6/6/6\r\n", NULL);
    
        sendmail_write(sock, "\r\n",              NULL);
    
        sendmail_write(sock, "%s\r\n",            body);    // data
    
        sendmail_write(sock, ".\r\n",             NULL);    // end data
    	
        sendmail_write(sock, "QUIT",         NULL);    // terminate
    	printf("%s", "\nWe are after quit command"); //never even gets to this line
    
        #ifdef WIN32
        closesocket(sock);
        #else
        close(sock);
        #endif
    
        return 0;
    }
    
    int main(int argc, char *argv[]) {
    
    	
        
        int ret = sendmail(
            "[email protected]",   /* from     */
            "[email protected]",       /* to       */
            "Subject",              /* subject  */
            "body",                 /* body     */
            "mx2.hotmail.com",             /* hostname */
            25                      /* port     */
        );
    
        if (ret != 0)
            fprintf(stderr, "Failed to send mail (code: %i).\n", ret);
        else
            fprintf(stdout, "Mail successfully sent.\n");
    
        return ret;
    }

  8. #23
    id id
    Guest
    If you look at the code project log file you will notice that there is no reply after the first two of these sends (so your function will wait for ever to recv data that is not coming!). Either combine them into one send or add an option to turn off the recv in your sendmail_write function.

    Code:
        sendmail_write(sock, "\r\n",              NULL); // no recv after this
    
        sendmail_write(sock, "%s\r\n",            body);  // no recv after this
        sendmail_write(sock, ".\r\n",             NULL);    // recv after this
    
    Possible fix:
        sendmail_write(sock, "\r\n%s\r\n.\r\n",            body);

  9. #24
    id id
    Guest
    PS. Also no reply after following sends. Alter your function.

    Code:
    sendmail_write(sock, "From: %s\r\n",      from);
    sendmail_write(sock, "To: %s\r\n",        to);
    sendmail_write(sock, "Subject: %s\r\n",   subject);
    sendmail_write(sock, "Date: 6/6/6\r\n", NULL);
    
    FIX:
    sendmail_write(const int  sock,const char *str,const char *arg, BOOL bGetReply)

  10. #25
    Registered User deltabird's Avatar
    Join Date
    Jan 2003
    Posts
    73
    Ok, the program finishes, but I still get no e-mail from it. I did what id id said (thanks), so the program completes, but I get no e-mail! Here's the new code:
    Code:
    #include <stdio.h>
    #include <io.h>
    
    int  rc;
        char buf[256];
    
    //#define LINUX /* define this if you are on linux   */
    //#define WIN32 /* define this if you are on windows */
    
    #ifdef WIN32
    #       include "io.h"
    #       include "winsock2.h"      /* WSAGetLastError, WSAStartUp  */
    #       define snprintf _snprintf
    #endif
    
    #ifdef LINUX
    #       include <netdb.h>         /* gethostbyname  */
    #       include <netinet/in.h>    /* htons          */
    #       include <sys/socket.h>
    #endif
    
    #pragma comment(lib, "wsock32.lib")
    
    static void sendmail_write(const int  sock,const char *str,const char *arg, bool reply) 
    {
        char buf[4096];
    
        if (arg != NULL)
            snprintf(buf, sizeof(buf), str, arg);        
        else
            snprintf(buf, sizeof(buf), str);
        
        send(sock, buf, strlen(buf), 0); 
    	
    	if(reply)
    	{
    		if((rc = recv(sock, buf, sizeof(buf) - 1, 0)) == -1) 
    		{
    			perror("recv");
    			printf("%s", "ERROR");
    		} 
    		else 
    		{
    			buf[rc] = 0;
    			printf("%s", buf);
    		}
    	}
    }
    
    static int sendmail(const char *from,const char *to,const char *subject,const char *body, const char *hostname,const int   port)
    {
        struct hostent *host;   
        struct sockaddr_in saddr_in;
        int sock = 0;
    
    #ifdef WIN32
    	WSADATA wsaData;
    	if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) 
    	{
    		return -1;
    	}
    #endif
    
        sock = socket(AF_INET, SOCK_STREAM, 0);
    	host = gethostbyname(hostname);
    	
    	saddr_in.sin_family      = AF_INET;
    	saddr_in.sin_port        = htons((u_short)port);
    	saddr_in.sin_addr.s_addr = 0;
    
    	memcpy((char*)&(saddr_in.sin_addr), host->h_addr, host->h_length);
    
    	if(connect(sock, (struct sockaddr*)&saddr_in, sizeof(saddr_in)) == -1)
    	{
    		return -2;
    	}
        
        sendmail_write(sock, "helo %s\r\n",       from, true);    // greeting
        sendmail_write(sock, "mail from: %s\r\n", from, true);    // from
        sendmail_write(sock, "rcpt to: %s\r\n",   to, true);      // to
        sendmail_write(sock, "data\r\n",        NULL, true);    // begin data
    	//printf("%s", "\nWe are after quit command");
    
        // next comes mail headers
        sendmail_write(sock, "From: %s\r\n",      from, false);
        sendmail_write(sock, "To: %s\r\n",        to, false);
        sendmail_write(sock, "Subject: %s\r\n",   subject, false);
    	sendmail_write(sock, "Date: 6/6/6\r\n", NULL, false);
    
        sendmail_write(sock, "\r\n",              NULL, false);
    
        sendmail_write(sock, "%s\r\n",            body, false);    // data
    
        sendmail_write(sock, ".\r\n",             NULL, false);    // end data
    	
        sendmail_write(sock, "QUIT",         NULL, false);    // terminate
    	//printf("%s", "\nWe are after quit command"); //never even gets to this line
    
        #ifdef WIN32
        closesocket(sock);
        #else
        close(sock);
        #endif
    
        return 0;
    }
    
    int main(int argc, char *argv[]) {
    
    	
        
        int ret = sendmail(
            "[email protected]",   /* from     */
            "[email protected]",       /* to       */
            "Subject",              /* subject  */
            "body",                 /* body     */
            "mx2.hotmail.com",             /* hostname */
            25                      /* port     */
        );
    
        if (ret != 0)
            fprintf(stderr, "Failed to send mail (code: %i).\n", ret);
        else
            fprintf(stdout, "Mail successfully sent.\n");
    
        return ret;
    }
    Someone please try running it on their machine, put in your own address if you want and stuff and see if you get the e-mail. Thanks.

  11. #26
    Registered User deltabird's Avatar
    Join Date
    Jan 2003
    Posts
    73
    any more ideas? has anyone made a real working example of a program like this? can you compile and run it on your machine?

  12. #27
    id id
    Guest

    Unhappy

    This last one should be true to get the reply.
    Code:
      sendmail_write(sock, ".\r\n",             NULL, false);
    Try a different from address (different from the to address).
    Post the output again if still not working. Copy dos box by highlighting and pressing enter.

  13. #28
    id id
    Guest
    Solution pending...
    Please wait.

  14. #29
    id id
    Guest

    Angry

    The good news is that I got it working. Simply add the following directly below the connect statement:
    Code:
    	char buf[4096];
    
    	//read out server welcome message
    	if((rc = recv(sock, buf, sizeof(buf) - 1, 0)) == -1) 
    	{
    		perror("recv");
    		printf("%s", "ERROR");
    	} 
    	else 
    	{
    		buf[rc] = 0;
    		printf("%s", buf);
    	}
    You'll have a few emails :-)

    The bad news:
    PS!!!!


    You do not appear to be reading out the server welcome message. Do a recv call to remove this from the input buffer before sending anything.
    Add a printf so you see what you are sending out.
    The first one solved the problem. The second one would have made the problem obvious (as it did for me when I did it).

    Anyway, glad we finally solved this.

  15. #30
    Registered User deltabird's Avatar
    Join Date
    Jan 2003
    Posts
    73
    Yes! Thank you id id! Thank you so much! It finally works! You are my god! wooohoo!

    btw, you should register, id id

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble with DMA Segmentation Faults
    By firestorm717 in forum C Programming
    Replies: 2
    Last Post: 05-07-2006, 09:20 PM
  2. mail application
    By terracota in forum Networking/Device Communication
    Replies: 6
    Last Post: 07-31-2004, 04:36 PM
  3. Yahoo! Mail launch NEW features
    By beely in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 06-21-2004, 10:05 PM
  4. Mail Accounts?
    By ZakkWylde969 in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 06-20-2004, 12:57 PM
  5. the MAIL command in UNIX and Pipes
    By RoshanX in forum Linux Programming
    Replies: 19
    Last Post: 10-21-2003, 10:40 PM