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="caiss.cuny@gmail.com";
25 char *to_id="quaere1verum@gmail.com";
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 }
|