【C++】【网络】利用Socket 编写SMTP 发送邮件程序

#include #include #include #include using namespace std; #define WSWENS MAKEWORD(2,0) int main() { sockaddr_in sin; WSADATA wsadata; if(WSAStartup(WSWENS, &wsadata) !=0) cout<<"startup failed"<h_addr_list[0], hptr->h_length); printf("IP of smpt.sina.com is : %d:%d:%d:%d", sin.sin_addr.S_un.S_un_b.s_b1, sin.sin_addr.S_un.S_un_b.s_b2, sin.sin_addr.S_un.S_un_b.s_b3, sin.sin_addr.S_un.S_un_b.s_b4); cout << endl; if(connect(s, (sockaddr*)&sin, sizeof(sin))) { cout << "connect failed" << endl; exit(0); } else { cout << "connect success" << endl; } char bufferresv[10240]; int len = recv(s, bufferresv, 10240, 0); bufferresv[len]=0; printf("%s",bufferresv); cout << endl; // send "helo ehlo" char bufferHello[] = "helo ehlo/r/n"; cout << "helo ehlo..." << endl; send(s, bufferHello, strlen(bufferHello), 0); len = recv(s, bufferresv, 10240, 0); bufferresv[len]=0; printf("%s",bufferresv); cout << endl; // send "auth login" char bufferLogin[] = "auth login/r/n"; cout << "auth login..." << endl; send(s, bufferLogin, strlen(bufferLogin),0); len = recv(s, bufferresv, 10240, 0); bufferresv[len]=0; printf("%s",bufferresv); cout << endl; // send "username" send "psw" char bufferUserName[] = "c3VubnlrYWhv/r/n"; char bufferpsw[] = "MjAwNzE2MTA0MDk=/r/n"; cout << "input username...." << endl; send(s,bufferUserName,strlen(bufferUserName),0); len = recv(s, bufferresv, 10240, 0); bufferresv[len]=0; printf("%s",bufferresv); cout << endl; cout << "input password..." << endl; send(s,bufferpsw,strlen(bufferpsw),0); len = recv(s, bufferresv, 10240, 0); bufferresv[len]=0; printf("%s",bufferresv); cout << endl; // mail from: char bufferMailFrom[] = "mail from:/r/n"; cout << "mail from:..." << endl; send(s, bufferMailFrom, strlen(bufferMailFrom), 0); len = recv(s, bufferresv, 10240, 0); bufferresv[len]=0; printf("%s",bufferresv); cout << endl; // rcpt to: char bufferRcptTo[] = "rcpt to:/r/n"; cout << "rcpt to:..." << endl; send(s, bufferRcptTo, strlen(bufferRcptTo), 0); len = recv(s, bufferresv, 10240, 0); bufferresv[len]=0; printf("%s",bufferresv); cout << endl; // data char bufferData[] = "data/r/n"; cout << "data..." << endl; send(s, bufferData,strlen(bufferData), 0); len = recv(s, bufferresv, 10240, 0); bufferresv[len]=0; printf("%s",bufferresv); cout << endl; // char bufferTo[] = "to:[email protected]/r/n"; char bufferFrom[] = "from:[email protected]/r/n"; cout << "to:[email protected]... " << endl; send(s, bufferTo, strlen(bufferTo),0); cout << "from:[email protected]..." << endl; send(s, bufferFrom, strlen(bufferFrom), 0); char bufferSubject[] = "subject:hello from kaho/r/n"; cout << "subject:hello from kaho..." << endl; send(s, bufferSubject, strlen(bufferSubject), 0); char bufferPoint[] = "./r/n"; send(s, bufferPoint, strlen(bufferPoint), 0); len = recv(s, bufferresv, 10240, 0); bufferresv[len]=0; printf("%s",bufferresv); cout << endl; }  

 

以上代码感谢超哥像春哥般的光环照耀下做出来的...

 

如果想引用博客内容,请在明显的地方注释清楚转帖并且包含我们原帖的地址引用,谢谢,如果有任何问题,可以在日志下面留言。我尽力解答。

原文网址:http://blog.csdn.net/sunnykaho/archive/2009/12/02/4927178.aspx

 

你可能感兴趣的:(【网络】,【C++】)