Thread: how to send an Email using C/C++

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    10

    Red face how to send an Email using C/C++

    can anybody tell me how to simply send email using C/C++ Console Application on windows platform.. I hv tried gmail smtp without the Auth but its not working properly.. here is the code I have tried... If you guys hv working code instead of this pls put it here.. thanks a lot.

    SMTP log file says..

    Code:
    The IP you're using to send mail is not authorized to send email directly to our servers. Please use the SMTP relay at your service provider instead. Learn more at http://mail.google.com/support/bin/answer.py?answer=quit

    relevant part of code

    Code:
    #include <windows.h>
    #include <stdio.h>
    #include <winuser.h>
    #include <windowsx.h>
    #include <time.h>
    
    /*If you don't know the mail exchange server for an address for the following 
    "nslookup -querytype=mx gmail.com" but replace gmail.com with the domain for 
    whatever email address you want. YOU MUST CHANGE  THESE SETTINGS OR
    IT WILL NOT WORK!!! */
    
    #define BUFSIZE 800
    #define waittime 500
    #define cmailserver "gmail-smtp-in.l.google.com"
    #define cemailto "[email protected]"
    #define cemailfrom "[email protected]"
    #define LogLength 100
    #define SMTPLog "smtp.log"
    #define cemailsubject "Logged"
    
    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
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Did you read the code you posted, particularly this part:
    Code:
    
    /*If you don't know the mail exchange server for an address for the following 
    "nslookup -querytype=mx gmail.com" but replace gmail.com with the domain for 
    whatever email address you want. YOU MUST CHANGE  THESE SETTINGS OR
    IT WILL NOT WORK!!! */
    

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by harshadura View Post
    can anybody tell me how to simply send email using C/C++ Console Application on windows platform.. I hv tried gmail smtp without the Auth but its not working properly.. here is the code I have tried... If you guys hv working code instead of this pls put it here.. thanks a lot.

    SMTP log file says..

    Code:
    The IP you're using to send mail is not authorized to send email directly to our servers. Please use the SMTP relay at your service provider instead. Learn more at http://mail.google.com/support/bin/answer.py?answer=quit
    Oh the joys of scoop and poop code...

    Did you go to the url suggested? Also read tabstop's message...

    Most likely it doesn't recognize your proggy as a valid email client, think's you're trying to spam and won't let you send.
    Your Internet Service Provider (ISP) should have an smtp sender set up for their clients... usually smtp.<ispname>.<domain>... eg: smtp.frogsleggs.net that you can use.

  4. #4
    Registered User
    Join Date
    Aug 2011
    Posts
    10
    yeah friend.. i read that and did the particular changes regarding with it.. but no effect.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Normally I would suggest you get wireshark and trace what a real email client does.

    Then simulate that in your code.

    > strncat(line,emailfrom,strlen(emailfrom));
    strncat doesn't automatically append a \0, so the result may not be a proper 'C' string.
    So any following str... functions could be off in the weeds somewhere, trashing someone else's memory.

    Unless you also go to the trouble of keeping track of how much of the destination buffer you've actually used, strncat offers no safety over strcat.

    > sleep(waittime);
    Any code such as this which calls any kind of delay is basically broken.
    Ideally, the code should use select() with a timeout to monitor activity on the stream, and call recv() when there is something to read.
    The timeout can be used to detect when the other end has gone "unresponsive" as opposed to dying (which you don't detect either).

    sleep() just adds needless delay when things are working well, and just doesn't solve the problem when things are going slow.

    > err=recv(sockfd,Rec_Buf,bufsize,0);Rec_Buf[err] = '\0';
    And if err is -1 on an error, then you have a buffer underrun problem.
    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.

  6. #6
    Registered User
    Join Date
    Aug 2011
    Posts
    10
    Quote Originally Posted by CommonTater View Post
    Oh the joys of scoop and poop code...

    Did you go to the url suggested? Also read tabstop's message...

    Most likely it doesn't recognize your proggy as a valid email client, think's you're trying to spam and won't let you send.
    Your Internet Service Provider (ISP) should have an smtp sender set up for their clients... usually smtp.<ispname>.<domain>... eg: smtp.frogsleggs.net that you can use.
    friend i dont hv another Smtp to use.. the only Im able to use is Google mail smtp.. So how can i get worked with that..btw I forgot to tell that this method is not authorized pretty well. so the email will be directly reqesting to send the bulk emails..Is their any correct way to send email using gmail smtp with Authorized username passwords of a gmail account

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Yeah, good luck with that.

  8. #8
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by harshadura View Post
    friend i dont hv another Smtp to use.. the only Im able to use is Google mail smtp.. So how can i get worked with that..btw I forgot to tell that this method is not authorized pretty well. so the email will be directly reqesting to send the bulk emails..Is their any correct way to send email using gmail smtp with Authorized username passwords of a gmail account
    Hmm.... I would suggest you explain yourself here.

    Quote Originally Posted by Forum Guidlines
    6. Messages relating to cracking, (erroneously called "hacking" by many), copyright violations, or other illegal activities will be deleted. Due to the overlapping boundaries of code with malicious intent, and other legitimate uses of it, the moderators will assess each potential infraction on a case by case basis.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by harshadura View Post
    friend i dont hv another Smtp to use.. the only Im able to use is Google mail smtp..
    Are you telling me your ISP doesn't have email servers? You may want to look into a new provider.

    So how can i get worked with that..btw I forgot to tell that this method is not authorized pretty well. so the email will be directly reqesting to send the bulk emails..Is their any correct way to send email using gmail smtp with Authorized username passwords of a gmail account
    Bulk emails... Nope. Sorry, not going to help you become a spammer...

  10. #10
    Registered User
    Join Date
    Aug 2011
    Posts
    10
    Quote Originally Posted by CommonTater View Post
    Are you telling me your ISP doesn't have email servers? You may want to look into a new provider.



    Bulk emails... Nope. Sorry, not going to help you become a spammer...

    no noo its not like that.. i want to mail only to a one person email.. but may need to send emails frequently.. pls dont misunderstand I am nt gonna Spam..

  11. #11
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by harshadura View Post
    no noo its not like that.. i want to mail only to a one person email.. but may need to send emails frequently.. pls dont misunderstand I am nt gonna Spam..
    Well, you can always send them manually to that 'one' person.

  12. #12
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by manasij7479 View Post
    Well, you can always send them manually to that 'one' person.
    LOL.......but what if he wants to email his friend once a second? That's a completely reasonable request isn't it?
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  13. #13
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by AndrewHunter View Post
    LOL.......but what if he wants to email his friend once a second? That's a completely reasonable request isn't it?
    Maybe he has a robot and wants to email directions to it !

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Or sending log files / status reports from some server to yourself, so you can keep an eye on it.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to send Email By C Program ??
    By Gaurav Singh in forum C Programming
    Replies: 20
    Last Post: 03-01-2011, 04:23 PM
  2. how to send email using c
    By moussa in forum C++ Programming
    Replies: 7
    Last Post: 05-28-2008, 08:19 PM
  3. Send email
    By Abda92 in forum Windows Programming
    Replies: 4
    Last Post: 12-29-2007, 09:09 AM
  4. Send email via c++
    By davidboja in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2007, 08:51 AM
  5. How to Send Email
    By sampatel in forum C Programming
    Replies: 1
    Last Post: 11-23-2002, 02:07 PM