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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
| #include <iostream> #include <string> #include <WinSock2.h> #include <stdio.h> #include <unistd.h>
using namespace std; #pragma comment(lib, "ws2_32.lib")
void mail(){ char buff[50000]; string message; string info; string subject; WSADATA wsaData; WORD wVersionRequested = MAKEWORD(2, 1); int err = WSAStartup(wVersionRequested, &wsaData); SOCKADDR_IN addrServer; HOSTENT *pHostent; SOCKET sockClient;
cout << "你想查看邮件还是发邮件?\n\t1.查看邮箱\n\t2.发送邮件\n"; int call; cin >> call; if (call == 2) { sockClient = socket(AF_INET, SOCK_STREAM, 0); pHostent = gethostbyname("smtp.qq.com"); addrServer.sin_addr.S_un.S_addr = *((DWORD *) pHostent->h_addr_list[0]); addrServer.sin_family = AF_INET; addrServer.sin_port = htons(25); err = connect(sockClient, (SOCKADDR *) &addrServer, sizeof(SOCKADDR)); buff[recv(sockClient, buff, 500, 0)] = '\0';
message = "ehlo qq.com\r\n"; send(sockClient, message.c_str(), message.length(), 0); buff[recv(sockClient, buff, 500, 0)] = '\0';
message = "auth login\r\n"; send(sockClient, message.c_str(), message.length(), 0); buff[recv(sockClient, buff, 500, 0)] = '\0';
message = "MTAyNTE0MjkzMg==\r\n"; send(sockClient, message.c_str(), message.length(), 0); buff[recv(sockClient, buff, 500, 0)] = '\0';
message = "bXhkeXd1ZnVhcHVnYmVlag==\r\n"; send(sockClient, message.c_str(), message.length(), 0); buff[recv(sockClient, buff, 500, 0)] = '\0';
string mail; cout << "请输入收件人邮箱:"; cin >> mail;
message = "MAIL FROM:<1025142932@qq.com> \r\nRCPT TO:<"; message.append(mail); message.append("> \r\n"); send(sockClient, message.c_str(), message.length(), 0); buff[recv(sockClient, buff, 500, 0)] = '\0'; cout <<"5" << buff << endl; buff[recv(sockClient, buff, 500, 0)] = '\0';
message = "DATA\r\n"; send(sockClient, message.c_str(), message.length(), 0); buff[recv(sockClient, buff, 500, 0)] = '\0'; message = "From: 1025142932@qq.com\r\nTo: " + mail + "\r\nsubject:"; cout << "主题:"; cin >> subject; message.append(subject); message.append("\r\n\r\n"); cout << "内容:"; cin >> info; message.append(info); message.append("\r\n.\r\n");
send(sockClient, message.c_str(), message.length(), 0); message = "QUIT\r\n"; send(sockClient, message.c_str(), message.length(), 0); buff[recv(sockClient, buff, 500, 0)] = '\0';
cout << "发送成功!" << endl; } if (call == 1) { sockClient = socket(AF_INET, SOCK_STREAM, 0); const char *host_id = "pop3.126.com"; pHostent = gethostbyname("pop.qq.com"); int port = 110; addrServer.sin_addr.S_un.S_addr = *((DWORD *) pHostent->h_addr_list[0]); addrServer.sin_family = AF_INET; addrServer.sin_port = htons(port); err = connect(sockClient, (SOCKADDR *) &addrServer, sizeof(SOCKADDR)); buff[recv(sockClient, buff, 500, 0)] = '\0';
message = "user 1025142932@qq.com\r\n"; send(sockClient, message.c_str(), message.length(), 0); buff[recv(sockClient, buff, 500, 0)] = '\0';
message = "pass mxdywufuapugbeej\r\n"; send(sockClient, message.c_str(), message.length(), 0); buff[recv(sockClient, buff, 500, 0)] = '\0';
message = "stat\r\n"; send(sockClient, message.c_str(), message.length(), 0); buff[recv(sockClient, buff, 500, 0)] = '\0';
message = "list\r\n"; send(sockClient, message.c_str(), message.length(), 0); buff[recv(sockClient, buff, 50000, 0)] = '\0';
cout << "Client : send list \nServer :" << buff << endl; while(1) { int n,option; cout << "你先想查看那一封邮件?输入序号" << endl; cin >> n; message = "retr " + to_string(n) + "\r\n";
send(sockClient, message.c_str(), message.length(), 0);
cout << "Client : send retr (...) \n";
buff[recv(sockClient, buff, 50000, 0)] = '\0'; cout << "Server :" << buff << endl;
cout<<"1.继续查看 2.退出"; cin>>option; if(option == 1) ; else break; } } }
int main() { while(1){ mail(); } }
|