how to send an Email using C/C++

DURApix

Well-known member
  • Aug 1, 2010
    1,259
    192
    63
    /dev/null
    මචංලා මට C or C++ ‍යුස් කරල Email එකක් යවාගන්න ඕනි. Normal Windows Console Application එහෙකින්..
    ගොඩක් ට්‍රයි කලා දවසක් විතර ඒත් හරි ගියෙ නෑ... :(
    කරුණාකරල දන්න යාලුවෙක් උදව්වක් දෙන්න.. නැත්තම් බම්ප් එකක්වත් දාල යන්න..:dull:
    එලකිරි මචංලා...
     

    DURApix

    Well-known member
  • Aug 1, 2010
    1,259
    192
    63
    /dev/null
    I've a tool But cannot give it away lol it's copy right reserved by my work place..

    are you trying to do it via a local mail server?

    no machan Im gonna use gmail smtp..this is their what i hv tried but its not working properly...the 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


    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"

    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
    }
     

    twisted

    Well-known member
  • Feb 21, 2008
    34,398
    818
    113
    upon purple clouds
    no machan Im gonna use gmail smtp..this is their what i hv tried but its not working properly...the 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
    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"
    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
    }

    apparently they are not letting to you directly administrate mails thru their servers like that..were you able to do the same thru google mail server in vb?
     

    ARDS

    Active member
  • Oct 14, 2010
    599
    32
    28
    cyber space
    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

    the log says u hve to use the authentication true para.. dnt knw whr it use.. bt i think dats the prob..