Send email from C++ Code ( No libs )

Hello all,

I am trying to send a simple text email from and through gmail.
I found some code online.
Originally I got a problem stating that I needed to 'STARTTTL', but I fixed it.


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
124
 loginwindow.cpp  smtp.cpp                                                                                                                                                                     X
  1 #include<iostream> 
  2 #include <sys/types.h> 
  3 #include <sys/socket.h> 
  4 #include <netinet/in.h> 
  5 #include <netdb.h> 
  6 #include <stdio.h> 
  7 #include<cstring> 
  8 #include<unistd.h> 
  9 #include<cstdlib> 
 10  
 11 using namespace std; 
 12 #define HELO "HELO smtp.gmail.com\r\n" 
 13 #define DATA "DATA\r\n" 
 14 #define QUIT "QUIT\r\n" 
 15  
 16 //#define h_addr h_addr_list[0] 
 17 //FILE *fin; 
 18 int sock; 
 19 struct sockaddr_in server; 
 20 struct hostent *hp, *gethostbyname(); 
 21 char buf[BUFSIZ+1]; 
 22 int len; 
 23 char *host_id="smtp.gmail.com";  //192.168.1.10"; 
 24 char *from_id="[email protected]"; 
 25 char *to_id="[email protected]"; 
 26 char *sub="testmail\r\n"; 
 27 char wkstr[100]="hello how r u\r\n"; 
 28  
 29 /*=====Send a string to the socket=====*/ 
 30  
 31 void send_socket(char *s) 
 32 { 
 33         write(sock,s,strlen(s)); 
 34         write(1,s,strlen(s)); 
 35         //printf("Client:%s\n",s); 
 36 } 
 37  
 38 //=====Read a string from the socket=====*/ 
 39  
 40 void read_socket() 
 41 { 
 42         len = read(sock,buf,BUFSIZ); 
 43         write(1,buf,len); 
 44         //printf("Server:%s\n",buf); 
 45 } 
 46  
/*=====MAIN=====*/
 48 int main(int argc, char* argv[])
 49 {
 50         /*=====Create Socket=====*/
 51         sock = socket(AF_INET, SOCK_STREAM, 0);
 52 
 53         if (sock==-1)
 54         {
 55                 perror("opening stream socket");
 56                 exit(1);
 57         }
 58         else
 59                 cout << "socket created\n";
 60 
 61         /*=====Verify host=====*/
 62         server.sin_family = AF_INET;
 63         cout<<"About to call gethost ... \n";
 64         hp = gethostbyname(host_id);
 65 
 66         cout<<"Return from gethost ... "<< hp<<"\n";
 67         if (hp==(struct hostent *) 0)
 68         {
 69                 fprintf(stderr, "%s: unknown host\n", host_id);
 70                 exit(2);
 71         }
 72         cout<<"Trying to connect to remote host\n";
 73 
 74         /*=====Connect to port 25 on remote host=====*/
 75         memcpy((char *) &server.sin_addr, (char *) hp->h_addr, hp->h_length);
 76 
 77         server.sin_port=htons(25); /* SMTP PORT */
 78 
 79         cout<<"Trying to connect to remote host now\n";
 80         if (connect(sock, (struct sockaddr *) &server, sizeof server)==-1)
 81         {
 82                 perror("connecting stream socket");
 83                 exit(1);
 84         }
 85         else
 86                 cout << "Connected\n";
 87 
 88         /*=====Write some data then read some =====*/
 89         read_socket(); /* SMTP Server logon string */
 90         send_socket("STARTTLS\n");
 91         read_socket(); /*Read reply */
 93         send_socket(HELO); /* introduce ourselves */
 94         read_socket(); /*Read reply */
 95 
 96         send_socket("STARTTLS\n");
 97 
 98         send_socket("MAIL FROM: ");
 99         send_socket(from_id);
100         send_socket("\r\n");
101         read_socket(); /* Sender OK */
102         send_socket("VRFY ");
103         send_socket(from_id);
104         send_socket("\r\n");
105         read_socket(); // Sender OK */
106         send_socket("RCPT TO: "); /*Mail to*/
107         send_socket(to_id);
108         send_socket("\r\n");
109         read_socket(); // Recipient OK*/
110         send_socket(DATA);// body to follow*/
111         send_socket("Subject: ");
112         send_socket(sub);
113         read_socket(); // Recipient OK*/
114         send_socket(wkstr);
115         send_socket(".\r\n");
116         read_socket();
117         send_socket(QUIT); /* quit */
118         read_socket(); // log off */
119 
120         //=====Close socket and finish=====*/
121         close(sock);
122         exit(0);
123 }

Help. Don't I need to enter the sender's password also?
Last edited on
I am not sure but you will have to implement ssl. You may not get through with starttls to gmail. Probably, check if gmail is sending any errors back.

Did not read it correctly, I thought you are sending email to gmail.
Last edited on
No, I want to send emails from a gmail account to any arbitrary email.
You can't do it without external libraries as gmail uses SSL. So you either write your own SSL library (strongly advise you against that) or use an existing library like OpenSSL.

To make your life even easier I'd use libcurl and OpenSSL together, write few lines of code and your application is ready (if I'm not mistaken there is already example code on licurl website) :)

Also please note that gmail does not use port 25. SMTP service must already be activated from gmail settings web interface. You will find there all settings you need, like SMTP server address and port to connect to.
Last edited on
@modoran: Thank you.

If it is OpenSSL and libcurl it's, but I was thinking that someone would ask me to download something heavy like Boost or something.
Currently, the other part of the code is using openssl and maybe libcurl. I just don't know enough.

I will look into the libcurl website.

About the port 25, yeah I read that they use some three digit port for SSL connections.
I heard about manually activating the gmail setting, but I was unable to find the page. I will try again and report back.
Thanks again.
I think you need to confirm it.. to send outgoing email, ssl is not necessary. You can check it by configuring any email client like mozilla thunderbird by not using ssl for outgoing emails. It will still be able to send emails.
I think you need to confirm it.. to send outgoing email, ssl is not necessary. You can check it by configuring any email client like mozilla thunderbird by not using ssl for outgoing emails. It will still be able to send emails.


Gmail requires you to use SSL to connect to their SMTP server, you cannot bypass that, unless you use a proxy of some kind. But why bother ?

Of course, there are other mail providers that does not use SSL, but not gmail.
Topic archived. No new replies allowed.