POP邮件收取邮件 代码
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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 | // 111111.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <WinSock.h> #include <iostream> #include <string> #pragma comment(lib,"WSock32.Lib") class ZBase64 { public : std:: string Encode( const unsigned char * Data, int DataByte); std:: string Decode( const char * Data, int DataByte, int & OutByte); }; std:: string ZBase64::Encode( const unsigned char * Data, int DataByte) { //编码表 const char EncodeTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" ; //返回值 std:: string strEncode; unsigned char Tmp[4] = { 0 }; int LineLength = 0; for ( int i = 0; i<( int )(DataByte / 3); i++) { Tmp[1] = *Data++; Tmp[2] = *Data++; Tmp[3] = *Data++; strEncode += EncodeTable[Tmp[1] >> 2]; strEncode += EncodeTable[((Tmp[1] << 4) | (Tmp[2] >> 4)) & 0x3F]; strEncode += EncodeTable[((Tmp[2] << 2) | (Tmp[3] >> 6)) & 0x3F]; strEncode += EncodeTable[Tmp[3] & 0x3F]; if (LineLength += 4, LineLength == 76) { strEncode += "\r\n" ; LineLength = 0; } } //对剩余数据进行编码 int Mod = DataByte % 3; if (Mod == 1) { Tmp[1] = *Data++; strEncode += EncodeTable[(Tmp[1] & 0xFC) >> 2]; strEncode += EncodeTable[((Tmp[1] & 0x03) << 4)]; strEncode += "==" ; } else if (Mod == 2) { Tmp[1] = *Data++; Tmp[2] = *Data++; strEncode += EncodeTable[(Tmp[1] & 0xFC) >> 2]; strEncode += EncodeTable[((Tmp[1] & 0x03) << 4) | ((Tmp[2] & 0xF0) >> 4)]; strEncode += EncodeTable[((Tmp[2] & 0x0F) << 2)]; strEncode += "=" ; } return strEncode; } std:: string ZBase64::Decode( const char * Data, int DataByte, int & OutByte) { //解码表 const char DecodeTable[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 62, // '+' 0, 0, 0, 63, // '/' 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, // '0'-'9' 0, 0, 0, 0, 0, 0, 0, 0, 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, // 'A'-'Z' 0, 0, 0, 0, 0, 0, 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, // 'a'-'z' }; //返回值 std:: string strDecode; int nValue; int i = 0; while (i < DataByte) { if (*Data != '\r' && *Data != '\n' ) { nValue = DecodeTable[*Data++] << 18; nValue += DecodeTable[*Data++] << 12; strDecode += (nValue & 0x00FF0000) >> 16; OutByte++; if (*Data != '=' ) { nValue += DecodeTable[*Data++] << 6; strDecode += (nValue & 0x0000FF00) >> 8; OutByte++; if (*Data != '=' ) { nValue += DecodeTable[*Data++]; strDecode += nValue & 0x000000FF; OutByte++; } } i += 4; } else // 回车换行,跳过 { Data++; i++; } } return strDecode; } //=============================== class SocketInit { public : SocketInit() { WSADATA wsaData; int err; err = WSAStartup(MAKEWORD(2,2), &wsaData); if (err) { std::cerr << "socket init error " << err << std::endl; exit(err); } } ~SocketInit() { WSACleanup(); } }; SocketInit gSockInit; const int BUF_MAX_LEN = 1024*10; const char * strUser = "" ; //邮箱用户名 const char * strPassWord = "" ; //邮箱密码 int main() { // base64 解码示例 std:: string mailStr(R"(PGRpdiBzdHlsZT0ibGluZS1oZWlnaHQ6MS43O2NvbG9yOiMwMDAwMDA7Zm9udC1zaXplOjE0cHg7 Zm9udC1mYW1pbHk6QXJpYWwiPjxzcGFuIHN0eWxlPSJjb2xvcjogcmdiKDQ0LCA0NCwgNDQpOyBm b250LWZhbWlseTogy87M5SwgJnF1b3Q7QXJpYWwgTmFycm93JnF1b3Q7LCBhcmlhbCwgc2VyaWY7 IGxpbmUtaGVpZ2h0OiAyOHB4OyI+16LS4snPw+bK1dPKvP61xLn9s8yyorK70uLOttfFvavV4tCp 08q8/teq0sa1vcTjtcS8xsvju/rJz6GjxOPKx7XDtb3V4tCp08q8/rXE0ru33b+9sbSjrMTjtcTX odDFz+TW0MjUu+G05rfF1eLQqdPKvP7WsbW9xOPTw0RFTEXD/MHuyb6z/cv8w8eho82s0fm12KOs REVMRcP8we7SstDo0qrE48zhuanTyrz+tcTL99L9usWho8q1vMrJz6OstbHE48q508PBy0RFTEXD /MHuuvOjrNXi0KnTyrz+sqKyu8rHwaK8tLTTPC9zcGFuPjxhIGNsYXNzPSJjaGFubmVsX2tleWxp bmsiIGhyZWY9Imh0dHA6Ly93d3cuMzgwMGhrLmNvbS8iIHN0eWxlPSJwYWRkaW5nOiAwcHg7IG1h cmdpbjogMHB4OyBjb2xvcjogcmdiKDExNCwgNDEsI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wYW4+)"); ZBase64 t; int i; std:: string sOut = t.Decode(mailStr.c_str(), mailStr.size(), i); std::cout << sOut << std::endl; //========================================== SOCKET mySock = socket(AF_INET, SOCK_STREAM, 0); //创建了可识别套接字 struct hostent *remoteHost; struct in_addr addr; if ((remoteHost = gethostbyname( "pop.126.com" )) == NULL) exit(1); if (remoteHost->h_addrtype == AF_INET) { addr.s_addr = *(u_long *)remoteHost->h_addr_list[0]; printf( "IP Address #%d: %s\n" , 0, inet_ntoa(addr)); } SOCKADDR_IN popServer; popServer.sin_addr = addr; popServer.sin_family = AF_INET; popServer.sin_port = htons(110); // step 1 connect int ret = connect(mySock, (SOCKADDR*)&popServer, sizeof (SOCKADDR)); if (ret) exit(ret); char receiveBuf[BUF_MAX_LEN] = {0}; ret = recv(mySock, receiveBuf, BUF_MAX_LEN,0); if (SOCKET_ERROR == ret) exit(ret); std::cout << receiveBuf ; // step 2 sendUser std:: string SendMessage( "USER " ); SendMessage += strUser; SendMessage += "\r\n" ; ret = send(mySock, SendMessage.c_str(), SendMessage.size(),0); if (SOCKET_ERROR == ret) exit(ret); ret = recv(mySock, receiveBuf, BUF_MAX_LEN, 0); if (SOCKET_ERROR == ret) exit(ret); std::cout << receiveBuf; //stecp 3 sendPass SendMessage.clear(); SendMessage += "PASS " ; SendMessage += strPassWord; SendMessage += "\r\n" ; ret = send(mySock, SendMessage.c_str(), SendMessage.size(), 0); if (SOCKET_ERROR == ret) exit(ret); ret = recv(mySock, receiveBuf, BUF_MAX_LEN, 0); if (SOCKET_ERROR == ret) exit(ret); std::cout << receiveBuf; //stecp 4 test LIST SendMessage.clear(); SendMessage += "LIST\r\n" ; ret = send(mySock, SendMessage.c_str(), SendMessage.size(), 0); if (SOCKET_ERROR == ret) exit(ret); ret = recv(mySock, receiveBuf, BUF_MAX_LEN, 0); if (SOCKET_ERROR == ret) exit(ret); //std::cout << receiveBuf; std::cout << std::endl << std::endl << std::endl << std::endl << std::endl << std::endl << std::endl << std::endl << std::endl; /* //stecp 5 test DELE SendMessage.clear(); SendMessage += "DELE 2\r\n"; ret = send(mySock, SendMessage.c_str(), SendMessage.size(), 0); if (SOCKET_ERROR == ret) exit(ret); //ret = recv(mySock, receiveBuf, BUF_MAX_LEN, 0); if (SOCKET_ERROR == ret) exit(ret); std::cout << receiveBuf; //ret = recv(mySock, receiveBuf, BUF_MAX_LEN, 0); //if (SOCKET_ERROR == ret) //exit(ret); //std::cout << receiveBuf; */ //stecp 6 test RETR SendMessage.clear(); SendMessage += "RETR 1\r\n" ; ret = send(mySock, SendMessage.c_str(), SendMessage.size(), 0); if (SOCKET_ERROR == ret) exit(ret); ret = recv(mySock, receiveBuf, BUF_MAX_LEN, 0); if (SOCKET_ERROR == ret) exit(ret); std::cout << receiveBuf; std::cout << std::endl; ret = recv(mySock, receiveBuf, BUF_MAX_LEN, 0); if (SOCKET_ERROR == ret) exit(ret); std::cout << receiveBuf; std::cout << std::endl; closesocket(mySock); return 0; } |
参考此博客
http://yerik.blog.51cto.com/1662422/743998
作 者: itdef
欢迎转帖 请保持文本完整并注明出处
技术博客 http://www.cnblogs.com/itdef/
B站算法视频题解
https://space.bilibili.com/18508846
qq 151435887
gitee https://gitee.com/def/
欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
如果觉得不错,欢迎点赞,你的鼓励就是我的动力
欢迎转帖 请保持文本完整并注明出处
技术博客 http://www.cnblogs.com/itdef/
B站算法视频题解
https://space.bilibili.com/18508846
qq 151435887
gitee https://gitee.com/def/
欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
如果觉得不错,欢迎点赞,你的鼓励就是我的动力
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步