163郵箱登入
阿新 • • 發佈:2018-12-17
最近郵箱專案需要取163郵箱收件箱信件內容, 在網上找了半天沒找到滿意的, 由於剛入收C#, 好多庫不怎麼了解, 網上也搜不到, 於是自己造了一個"輪子"(美其名曰: 開發")
好了, 不多廢話, 迴歸正題.
登入
public bool LoginEmail() { string oldUrl = "https://reg.163.com/logins.jsp?username=" + username + "&password=" + password + "&url=http://fm163.163.com/coremail/fcg/ntesdoor2"; try { HttpWebRequest request = WebRequest.Create(oldUrl) as HttpWebRequest; request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"; request.UserAgent = "User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3086.0 Safari/537.36"; HttpWebResponse response = request.GetResponse() as HttpWebResponse; cookieContainer.Add(CookieUpdate(response.GetResponseHeader("Set-Cookie"))); StreamReader reader = new StreamReader(response.GetResponseStream()); string html = reader.ReadToEnd(); reader.Close(); string content = @"var url = """; int startIndex = html.IndexOf(content); int endIndex = html.IndexOf(@"""", startIndex + content.Length); string newUrl = html.Substring(startIndex + content.Length, endIndex - startIndex - content.Length); request = WebRequest.Create(newUrl) as HttpWebRequest; // 跳轉 request.Referer = oldUrl; request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"; request.UserAgent = "User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3086.0 Safari/537.36"; oldUrl = newUrl; request.CookieContainer = cookieContainer; response = request.GetResponse() as HttpWebResponse; string testText = response.GetResponseHeader("Set-Cookie"); cookieContainer.Add(CookieUpdate(response.GetResponseHeader("Set-Cookie"))); reader = new StreamReader(response.GetResponseStream()); html = reader.ReadToEnd(); reader.Close(); loginReferer = response.ResponseUri.OriginalString; int index = loginReferer.IndexOf("=") + 1; int nCount = loginReferer.LastIndexOf("&") - index; sid = loginReferer.Substring(index, nCount); return true; } catch (Exception ex) { throw ex; } }
獲取收件箱列表
public void getEmailList() { try { HttpWebRequest request = WebRequest.Create("http://mail.163.com/js6/s?sid=" + sid + "&func=mbox:listMessages&TopTabReaderShow=1&TopTabLofterShow=1&welcome_welcomemodule_mailrecom_click=1&LeftNavfolder1Click=1&mbox_folder_enter=1") as HttpWebRequest; request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"; request.Referer = loginReferer; request.UserAgent = "User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3086.0 Safari/537.36"; request.CookieContainer = cookieContainer; HttpWebResponse response = request.GetResponse() as HttpWebResponse; StreamReader reader = new StreamReader(response.GetResponseStream()); string emailIdText = reader.ReadToEnd(); reader.Close(); if (!emailIdText.Contains("<?xml version=")) { throw new IOException("郵箱登入失敗!"); } string parren = @"<string name=""id"">(.*?)</string>[\S\s]*?<string name=""from"">""(.*?)"""; Regex regex = new Regex(parren); if (!regex.IsMatch(emailIdText)) { throw new IOException("郵箱登入失敗!"); } MatchCollection matchCollection = regex.Matches(emailIdText); foreach (Match match in matchCollection) { GroupCollection groupCollection = match.Groups; if (groupCollection.Count < 3) continue; emailIdList.Add(new KeyValuePair<string, string>(groupCollection[2].ToString(), groupCollection[1].ToString())); } //GetEmailBody(); } catch (Exception ex) { throw ex; } }
本人正則不過關, 如有大神看見, 請略過......
獲取指定發件人信件內容
public string GetEmailBody(string name) { try { string emailId = getEmailKeyId(name); if (string.IsNullOrEmpty(emailId)) return null; string url = "http://mail.163.com/js6/read/readhtml.jsp?mid=" + emailId + "&userType=newuser&font=15&color=064977"; HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; request.CookieContainer = cookieContainer; request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"; request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"; request.Referer = loginReferer; HttpWebResponse response = request.GetResponse() as HttpWebResponse; StreamReader reader = new StreamReader(response.GetResponseStream()); string html = reader.ReadToEnd(); reader.Close(); return html; } catch (Exception ex) { throw ex; } }
宣告