上一篇文章http://blog.csdn.net/wangjiannuaa/article/details/6949368阐述了winInet基本使用方法。登录邮箱后干啥?当然是发邮件了~搞了半天终于把程序调通了。
首先还是分析真实的环境,邮件是怎么发送出去的,所谓分析,在这里就是抓包看数据了。
如下图所示,上一篇文章基础,也就是登录邮箱后,动作分解为三步骤:1.进入基本版-2.点击写信-3.点击发送。至于为什么是基本版,前文已有论述,是为了避免繁文缛节,把数据包尽可能得简化。
第一步,进入基本版:
GET http://m1.mail.sina.com.cn/basic/ HTTP/1.1
Accept: text/html, application/xhtml+xml, */*
Referer: http://m1.mail.sina.com.cn/basic/listmail.php?fid=new
Accept-Language: zh-CN
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; MAAU)
Accept-Encoding: gzip, deflate
Host: m1.mail.sina.com.cn
Connection: Keep-Alive
Cookie: [email protected]; SINAMAIL-WEBFACE-SESSID=4810c7c33fc1e1c6682bceab4ddef2c6; UOR=,,; U_TRS1=000000b4.2d6a4a30.4dbeccdf.f7085a45; vjuids=-8a3c2a43b.12f673b30db.0.8ab6cd7a91ca3; vjlast=1304434711; SINAGLOBAL=00000049.c6df3442.4e8ecea8.c10d0d3d; ULV=1320821256248:19:9:9:00000042.71d45d8d.4eba2208.9827aebf:1320816503894; ALLYESID4=00110503104143503706036; SGUP=0; mvsign=v%3D%2Atz%3Cc5uhs4CE5F_%24Uf5%3E; loginType=freemail; SHOW_TIP_BOX=1; _s_upa=7; ULOGIN_IMG=13208272802654; Apache=00000042.71d45d8d.4eba2208.9827aebf; SID=BdimHsH8SQooBHNNxsHHNHgo4oBBrWWBBoWHN8nirxHHNoWBgxW8yoW; SPRIAL=d38fb34629f58bca2d47db89f8bcc1f5; SINA_USER=j; U_TRS2=000000a2.1e3514bd.4eba226c.e1f9f60b; SUE=es%3D08613ec0baa20bc5de1c64800beebcd4%26ev%3Dv1%26es2%3Db3819624e7f5c93ed6e9ffd2f7667ccb%26rs0%3DegQRXaekdBX0Cv4RZLVGeN%252BJov2MOCrllf%252BT5K%252FHgqxOfu7Z3F8xlJHKpglceCpId1YzV%252FFh91g5tZvk86raopv99gWgxx%252FfAaEFoC6DeskUGWkmwKVO9yAspaUSOo4SPSzMlZX0Gy%252BkHtU%252BsOx7rVIVjNy6Zezg5oyexRYLOPk%253D%26rv%3D0; SUP=cv%3D1%26bt%3D1320827324%26et%3D1320913724%26lt%3D1%26uid%3D2521991044%26user%3Djimmy_cmd.%252A%252A%26ag%3D9%26name%3Djimmy_cmd%2540sina.com%26nick%3Djimmy_cmd%26sex%3D%26ps%3D0%26email%3Djimmy_cmd%2540sina.com%26dob%3D%26ln%3Djimmy_cmd%2540sina.com%26os%3D%26fmp%3D%26lcp%3D%26us%3D; SWEBAPPSESSID=ff951a6cf52f9a9cdfb8dfd7b67d0b8b; SWMHA=SWMHA137; WTP_VISSTATUS=TRUE; WTP_VISTAB=address
第二步,点击写信:
GET http://m1.mail.sina.com.cn/basic/writemail.php HTTP/1.1
Accept: text/html, application/xhtml+xml, */*
Referer: http://m1.mail.sina.com.cn/basic/
Accept-Language: zh-CN
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; MAAU)
Accept-Encoding: gzip, deflate
Host: m1.mail.sina.com.cn
Connection: Keep-Alive
Cookie: [email protected]; SINAMAIL-WEBFACE-SESSID=4810c7c33fc1e1c6682bceab4ddef2c6; UOR=,,; U_TRS1=000000b4.2d6a4a30.4dbeccdf.f7085a45; vjuids=-8a3c2a43b.12f673b30db.0.8ab6cd7a91ca3; vjlast=1304434711; SINAGLOBAL=00000049.c6df3442.4e8ecea8.c10d0d3d; ULV=1320821256248:19:9:9:00000042.71d45d8d.4eba2208.9827aebf:1320816503894; ALLYESID4=00110503104143503706036; SGUP=0; mvsign=v%3D%2Atz%3Cc5uhs4CE5F_%24Uf5%3E; loginType=freemail; SHOW_TIP_BOX=1; _s_upa=7; ULOGIN_IMG=13208272802654; Apache=00000042.71d45d8d.4eba2208.9827aebf; SID=BdimHsH8SQooBHNNxsHHNHgo4oBBrWWBBoWHN8nirxHHNoWBgxW8yoW; SPRIAL=d38fb34629f58bca2d47db89f8bcc1f5; SINA_USER=j; U_TRS2=000000a2.1e3514bd.4eba226c.e1f9f60b; SUE=es%3D08613ec0baa20bc5de1c64800beebcd4%26ev%3Dv1%26es2%3Db3819624e7f5c93ed6e9ffd2f7667ccb%26rs0%3DegQRXaekdBX0Cv4RZLVGeN%252BJov2MOCrllf%252BT5K%252FHgqxOfu7Z3F8xlJHKpglceCpId1YzV%252FFh91g5tZvk86raopv99gWgxx%252FfAaEFoC6DeskUGWkmwKVO9yAspaUSOo4SPSzMlZX0Gy%252BkHtU%252BsOx7rVIVjNy6Zezg5oyexRYLOPk%253D%26rv%3D0; SUP=cv%3D1%26bt%3D1320827324%26et%3D1320913724%26lt%3D1%26uid%3D2521991044%26user%3Djimmy_cmd.%252A%252A%26ag%3D9%26name%3Djimmy_cmd%2540sina.com%26nick%3Djimmy_cmd%26sex%3D%26ps%3D0%26email%3Djimmy_cmd%2540sina.com%26dob%3D%26ln%3Djimmy_cmd%2540sina.com%26os%3D%26fmp%3D%26lcp%3D%26us%3D; SWEBAPPSESSID=ff951a6cf52f9a9cdfb8dfd7b67d0b8b; SWMHA=SWMHA137; WTP_VISSTATUS=TRUE; WTP_VISTAB=address
第三步,点击发送:
POST http://m1.mail.sina.com.cn/basic/sendmail.php HTTP/1.1
Accept: text/html, application/xhtml+xml, */*
Referer: http://m1.mail.sina.com.cn/basic/writemail.php
Accept-Language: zh-CN
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; MAAU)
Content-Type: multipart/form-data; boundary=---------------------------7db2a861008aa
Accept-Encoding: gzip, deflate
Host: m1.mail.sina.com.cn
Content-Length: 1305
Connection: Keep-Alive
Pragma: no-cache
Cookie: [email protected]; SINAMAIL-WEBFACE-SESSID=4810c7c33fc1e1c6682bceab4ddef2c6; UOR=,,; U_TRS1=000000b4.2d6a4a30.4dbeccdf.f7085a45; vjuids=-8a3c2a43b.12f673b30db.0.8ab6cd7a91ca3; vjlast=1304434711; SINAGLOBAL=00000049.c6df3442.4e8ecea8.c10d0d3d; ULV=1320821256248:19:9:9:00000042.71d45d8d.4eba2208.9827aebf:1320816503894; ALLYESID4=00110503104143503706036; SGUP=0; mvsign=v%3D%2Atz%3Cc5uhs4CE5F_%24Uf5%3E; loginType=freemail; SHOW_TIP_BOX=1; _s_upa=7; ULOGIN_IMG=13208272802654; Apache=00000042.71d45d8d.4eba2208.9827aebf; SID=BdimHsH8SQooBHNNxsHHNHgo4oBBrWWBBoWHN8nirxHHNoWBgxW8yoW; SPRIAL=d38fb34629f58bca2d47db89f8bcc1f5; SINA_USER=j; U_TRS2=000000a2.1e3514bd.4eba226c.e1f9f60b; SUE=es%3D08613ec0baa20bc5de1c64800beebcd4%26ev%3Dv1%26es2%3Db3819624e7f5c93ed6e9ffd2f7667ccb%26rs0%3DegQRXaekdBX0Cv4RZLVGeN%252BJov2MOCrllf%252BT5K%252FHgqxOfu7Z3F8xlJHKpglceCpId1YzV%252FFh91g5tZvk86raopv99gWgxx%252FfAaEFoC6DeskUGWkmwKVO9yAspaUSOo4SPSzMlZX0Gy%252BkHtU%252BsOx7rVIVjNy6Zezg5oyexRYLOPk%253D%26rv%3D0; SUP=cv%3D1%26bt%3D1320827324%26et%3D1320913724%26lt%3D1%26uid%3D2521991044%26user%3Djimmy_cmd.%252A%252A%26ag%3D9%26name%3Djimmy_cmd%2540sina.com%26nick%3Djimmy_cmd%26sex%3D%26ps%3D0%26email%3Djimmy_cmd%2540sina.com%26dob%3D%26ln%3Djimmy_cmd%2540sina.com%26os%3D%26fmp%3D%26lcp%3D%26us%3D; SWEBAPPSESSID=ff951a6cf52f9a9cdfb8dfd7b67d0b8b; SWMHA=SWMHA137; WTP_VISSTATUS=TRUE; WTP_VISTAB=address
-----------------------------7db2a861008aa
Content-Disposition: form-data; name="to"
[email protected]
-----------------------------7db2a861008aa
Content-Disposition: form-data; name="cc"
-----------------------------7db2a861008aa
Content-Disposition: form-data; name="bcc"
-----------------------------7db2a861008aa
Content-Disposition: form-data; name="subject"
subject-basic
-----------------------------7db2a861008aa
Content-Disposition: form-data; name="atth1"; filename=""
Content-Type: application/octet-stream
-----------------------------7db2a861008aa
Content-Disposition: form-data; name="atth2"; filename=""
Content-Type: application/octet-stream
-----------------------------7db2a861008aa
Content-Disposition: form-data; name="atth3"; filename=""
Content-Type: application/octet-stream
-----------------------------7db2a861008aa
Content-Disposition: form-data; name="body"
hello-basic-version.
-----------------------------7db2a861008aa
Content-Disposition: form-data; name="saveSent"
1
-----------------------------7db2a861008aa
Content-Disposition: form-data; name="action"
-----------------------------7db2a861008aa
Content-Disposition: form-data; name="a_sendmail"
发 送
-----------------------------7db2a861008aa--
下面,就是要用WinInet API模拟以上三个过程就好了!
/****************************************************************** * 向奋战在刀光剑影中的特洛伊勇士们致敬 * 2011.11.07 * 于 南京航空航天大学 电子信息工程学院 ******************************************************************/ #include<windows.h> #include<wininet.h> #include <iostream> using namespace std; #pragma comment(lib,"wininet.lib") HINTERNET hSession = NULL; HINTERNET hConnect = NULL; HINTERNET hRequest = NULL; ////////////////////////////////////////////////////////////////////////// //构造邮件体 //注意行与行中间没有空格 ////////////////////////////////////////////////////////////////////////// void BuildMailContent(PSTR szFormData) { strcpy(szFormData,"\ -----------------------------7db2a861008aa\r\n\ Content-Disposition: form-data; name=\"to\"\r\n\ \r\n\ [email protected]\r\n\ -----------------------------7db2a861008aa\r\n\ Content-Disposition: form-data; name=\"cc\"\r\n\ \r\n\ [email protected]\r\n\ -----------------------------7db2a861008aa\r\n\ Content-Disposition: form-data; name=\"bcc\"\r\n\ \r\n\ \r\n\ -----------------------------7db2a861008aa\r\n\ Content-Disposition: form-data; name=\"subject\"\r\n\ \r\n\ subject-basic\r\n\ -----------------------------7db2a861008aa\r\n\ Content-Disposition: form-data; name=\"atth1\"; filename=\"\"\r\n\ Content-Type: application/octet-stream\r\n\ \r\n\ \r\n\ -----------------------------7db2a861008aa\r\n\ Content-Disposition: form-data; name=\"atth2\"; filename=\"\"\r\n\ Content-Type: application/octet-stream\r\n\ \r\n\ \r\n\ -----------------------------7db2a861008aa\r\n\ Content-Disposition: form-data; name=\"atth3\"; filename=\"\"\r\n\ Content-Type: application/octet-stream\r\n\ \r\n\ \r\n\ -----------------------------7db2a861008aa\r\n\ Content-Disposition: form-data; name=\"body\"\r\n\ \r\n\ application-send : send content!\r\n\ -----------------------------7db2a861008aa\r\n\ Content-Disposition: form-data; name=\"saveSent\"\r\n\ \r\n\ 1\r\n\ -----------------------------7db2a861008aa\r\n\ Content-Disposition: form-data; name=\"action\"\r\n\ \r\n\ \r\n\ -----------------------------7db2a861008aa\r\n\ Content-Disposition: form-data; name=\"a_sendmail\"\r\n\ \r\n\ 发 送\r\n\ -----------------------------7db2a861008aa--\r\n"); } ////////////////////////////////////////////////////////////////////////// //将返回的页面写入磁盘文件 ////////////////////////////////////////////////////////////////////////// BOOL WriteFileToDisk(LPCSTR lpFileName,HINTERNET& hInternet) { CHAR szRecvBuf[1024+1]; // 接受数据缓冲区 DWORD dwNumberOfBytesRead; // 服务器返回大小 DWORD dwRecvTotalSize=0; // 接受数据总大小 DWORD dwRecvBuffSize=0; // 接受数据buf的大小 BOOL bReturn = FALSE; memset(szRecvBuf,0,1024+1); DWORD dwNumberOfBytesWritten ; HANDLE hOpenFile = (HANDLE)CreateFileA(lpFileName,GENERIC_WRITE,0,NULL,OPEN_ALWAYS,NULL,NULL); if (hOpenFile == INVALID_HANDLE_VALUE) { hOpenFile = NULL; return FALSE; } do { bReturn = InternetReadFile(hInternet,szRecvBuf,1024,&dwNumberOfBytesRead); if(!bReturn) { break; } // 统计接受数据的大小 szRecvBuf[dwNumberOfBytesRead] = '\0'; dwRecvTotalSize += dwNumberOfBytesRead; dwRecvBuffSize += strlen(szRecvBuf); WriteFile(hOpenFile,szRecvBuf,dwNumberOfBytesRead,&dwNumberOfBytesWritten,NULL); } while (dwNumberOfBytesRead !=0); CloseHandle(hOpenFile); return TRUE; } ////////////////////////////////////////////////////////////////////////// //初始化HINTERNET 建立连接 ////////////////////////////////////////////////////////////////////////// BOOL InitionWinInet() { //InternetOpenA LPCSTR lpszAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; MAAU)"; DWORD dwAccessType = INTERNET_OPEN_TYPE_PRECONFIG; hSession = InternetOpenA(lpszAgent,dwAccessType,NULL,NULL,0); if (!hSession) { return FALSE; } return TRUE; } ////////////////////////////////////////////////////////////////////////// //模拟点击登录邮箱 ////////////////////////////////////////////////////////////////////////// BOOL ClickToLoginEmail() { //InternetConnectA LPCSTR lpszServerName = "mail.sina.com.cn"; INTERNET_PORT nServerPort = INTERNET_DEFAULT_HTTP_PORT; DWORD dwService = INTERNET_SERVICE_HTTP; hConnect = InternetConnectA(hSession,lpszServerName,nServerPort,NULL,NULL,dwService,0,1); if (!hConnect) { return FALSE; } //HttpOpenRequestA LPCSTR lpszVerb = "POST"; LPCSTR lpszObjectName = "cgi-bin/login.php"; //网站的对象名,网址在InternetConnectA第二个参数指定 LPCSTR lpszVersion = "HTTP/1.1"; LPCSTR lpszReferrer = "http://mail.sina.com.cn/cgi-bin/login.php"; LPCSTR lpszAcceptTypes = "text/html, application/xhtml+xml, */*"; DWORD dwFlags = INTERNET_FLAG_RELOAD; hRequest = HttpOpenRequestA(hConnect,lpszVerb,lpszObjectName,lpszVersion,lpszReferrer,&lpszAcceptTypes,dwFlags,1); if (!hRequest) { return FALSE; } //HttpSendRequestA LPCSTR lpszHeaders = "Content-Type: application/x-www-form-urlencoded\r\n"; DWORD dwHeadersLength=-1L; char szFormData[1024]={0}; strcpy(szFormData,"logintype=uid&u=jimmy_cmd&psw=123abc&product=mail&%B5%C7%C2%BC=%B5%C7+%C2%BC\r\n"); LPVOID pBuf = (LPVOID)szFormData; BOOL bReturn = HttpSendRequestA(hRequest,lpszHeaders,dwHeadersLength,pBuf,strlen(szFormData)); if(!bReturn) { return FALSE; } LPCSTR lpFileName = "login.html"; WriteFileToDisk(lpFileName,hRequest); return TRUE; } ////////////////////////////////////////////////////////////////////////// //模拟点击到基本版 ////////////////////////////////////////////////////////////////////////// BOOL ClickToBasicVersion() { LPCSTR lpszUrl = "http://m1.mail.sina.com.cn/basic/"; hRequest = InternetOpenUrlA(hSession,lpszUrl,NULL,0,INTERNET_FLAG_RELOAD,0); if (!hRequest) { return FALSE; } LPCSTR lpFileName = "basic.html"; WriteFileToDisk(lpFileName,hRequest); return TRUE; } ////////////////////////////////////////////////////////////////////////// //模拟点击写信按钮 ////////////////////////////////////////////////////////////////////////// BOOL ClickToWrite() { LPCSTR lpszUrl = "http://m1.mail.sina.com.cn/basic/writemail.php "; hRequest = InternetOpenUrlA(hSession,lpszUrl,NULL,0,INTERNET_FLAG_RELOAD,0); if (!hRequest) { return FALSE; } LPCSTR lpFileName = "write.html"; WriteFileToDisk(lpFileName,hRequest); return TRUE; } ////////////////////////////////////////////////////////////////////////// //模拟点击发送邮件 ////////////////////////////////////////////////////////////////////////// BOOL ClickSendEmail() { //InternetConnectA LPCSTR lpszServerName = "m1.mail.sina.com.cn"; INTERNET_PORT nServerPort = INTERNET_DEFAULT_HTTP_PORT; DWORD dwService = INTERNET_SERVICE_HTTP; hConnect = InternetConnectA(hSession,lpszServerName,nServerPort,NULL,NULL,dwService,0,1); if (!hConnect) { return FALSE; } //HttpOpenRequestA LPCSTR lpszVerb = "POST"; LPCSTR lpszObjectName = "basic/sendmail.php"; //网站的对象名,网址在InternetConnectA第二个参数指定 LPCSTR lpszVersion = "HTTP/1.1"; LPCSTR lpszReferrer = "http://m1.mail.sina.com.cn/basic/writemail.php"; LPCSTR lpszAcceptTypes = "text/html, application/xhtml+xml, */*"; DWORD dwFlags = INTERNET_FLAG_RELOAD; hRequest = HttpOpenRequestA(hConnect,lpszVerb,lpszObjectName,lpszVersion,lpszReferrer,&lpszAcceptTypes,dwFlags,1); if (!hRequest) { return FALSE; } //HttpSendRequestA LPCSTR lpszHeaders = "Content-Type: multipart/form-data; boundary=---------------------------7db2a861008aa\r\n"; DWORD dwHeadersLength=-1L; CHAR szContentData[10240]={0}; BuildMailContent(szContentData); LPVOID pBuf = (LPVOID)szContentData; BOOL bReturn = HttpSendRequestA(hRequest,lpszHeaders,dwHeadersLength,pBuf,strlen(szContentData)); if(!bReturn) { return FALSE; } return TRUE; } ////////////////////////////////////////////////////////////////////////// //主函数 ////////////////////////////////////////////////////////////////////////// void main() { BOOL isOK = FALSE ; do { if (!InitionWinInet()){ break; } cout<<"InitionWinInet finished."<<endl; if (!ClickToLoginEmail()){ break; } cout<<"ClickToLoginEmail finished."<<endl; if (!ClickToBasicVersion()){ break; } cout<<"ClickToBasicVersion finished."<<endl; if (!ClickToWrite()){ break; } cout<<"ClickToWrite finished."<<endl; if (!ClickSendEmail()){ break; } cout<<"ClickSendEmail finished."<<endl; } while (FALSE); cout<<"Press any key to quit."<<endl; getchar(); InternetCloseHandle(hSession); InternetCloseHandle(hConnect); InternetCloseHandle(hRequest); }