how to send an email using C/C++ with Auth Gmail Account

Aug 22, 2011 at 3:31pm
Hello,
I want to simply send email using C or C++.
I have tried so many examples and sample codes but they didn effect..

I hv tried to use the google smtp like a smtp replay(i mean pushed through
the gmails smtp relay) but gmail is rejecting my IP address and says like this


1
2
3
4
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?answequit               
. 



Can anyone tell me how to use separate Gmail account authorization and
work with this Email thing with C/C++ code..
(I mean suppose I have an Gmail Account called test@gmail.com,
I hv done this using Java..
Java allow to send emails through that authorized gmail account- by giving username and passwords to the Java code)

how to do this using C/C++?
pls help me guyz.
thanks
Last edited on Aug 22, 2011 at 3:32pm
Aug 23, 2011 at 1:35am
May I know what C/C++ library you use to do email sending? Or you do it the raw way connecting using sockets and do your own SMTP communication with the SMTP server ?
Aug 23, 2011 at 2:08am
yeah bro.. i want to do it using someway.. pls help.. thx
i am using devC++ btw
Aug 23, 2011 at 2:09am
You have not answered my question. Can you show the code snippets where your program is doing the email-ing stuff ?
Aug 23, 2011 at 2:27am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
        
        #include <windows.h>
        #include <stdio.h>
        #include <winuser.h>
        #include <windowsx.h>
        #include <time.h>
     
        #define BUFSIZE 800
        #define waittime 500
        #define cmailserver "gmail-smtp-in.l.google.com"
        #define cemailto "test@gmail.com"
        #define cemailfrom "test@gmail.com"
        #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
        }
        
Aug 23, 2011 at 2:36am
So your problem is not your program but more on the computer that execute your C++ program are being denied access to Gmail most presumably by your Internet service provider correct ?

Then the same computer that execute your Java program can get through correct ?

If above are true, it is very strange indeed. Are there any settings done at the Java end for which it is not done at the C++ end ? Hmm....
Topic archived. No new replies allowed.