| 1 |
#include <sockinet.h> |
| 2 |
#include <stdio.h> |
| 3 |
|
| 4 |
static void send_cmd(iosockstream&, const char* cmd=0); |
| 5 |
static void get_text(iosockstream&); |
| 6 |
|
| 7 |
int main(int ac, char** av) |
| 8 |
{ |
| 9 |
if (ac != 3) { |
| 10 |
cerr << "USAGE: " << av[0] << " hostname userid\n"; |
| 11 |
return 1; |
| 12 |
} |
| 13 |
|
| 14 |
iosockinet sio (sockbuf::sock_stream); |
| 15 |
|
| 16 |
sio->connect(av[1], "smtp", "tcp"); |
| 17 |
|
| 18 |
send_cmd(sio, 0); |
| 19 |
send_cmd(sio, "HELO kelvin.seas.virginia.edu"); |
| 20 |
send_cmd(sio, "HELP"); |
| 21 |
send_cmd(sio, "MAIL FROM:<test@test>"); |
| 22 |
|
| 23 |
char buf[512]; |
| 24 |
sprintf(buf, "RCPT TO:%s", av[2]); |
| 25 |
send_cmd(sio, buf); |
| 26 |
|
| 27 |
send_cmd(sio, "DATA"); |
| 28 |
cout << "terminate your input with cntrl-D\n\n"; |
| 29 |
while(cin.getline(buf, 511)) { |
| 30 |
if (buf[0] == '.') sio << '.'; |
| 31 |
sio << buf << "\r\n"; |
| 32 |
} |
| 33 |
sio << "\r\n.\r\n" << flush; |
| 34 |
send_cmd(sio, 0); |
| 35 |
|
| 36 |
|
| 37 |
send_cmd(sio, "HELP"); |
| 38 |
|
| 39 |
send_cmd(sio, "QUIT"); |
| 40 |
|
| 41 |
return 0; |
| 42 |
} |
| 43 |
|
| 44 |
void send_cmd(iosockstream& s, const char* cmd) |
| 45 |
{ |
| 46 |
if (cmd) { |
| 47 |
s << cmd << "\r\n" << flush; |
| 48 |
} |
| 49 |
get_text (s); |
| 50 |
} |
| 51 |
|
| 52 |
void get_text(iosockstream& s) |
| 53 |
{ |
| 54 |
char buf[256]; |
| 55 |
int tmo = s->recvtimeout(1); // wait for 1 sec before timing out |
| 56 |
while ( s.getline(buf, 255) ) { |
| 57 |
if (buf[0] == '.' && buf[1] == '\r' && buf[2] == '\n') break; |
| 58 |
cout << buf << endl; |
| 59 |
} |
| 60 |
if ( !s->is_eof() ) s.clear(); |
| 61 |
s->recvtimeout(tmo); //reset timeout to the previous value |
| 62 |
} |