您好,登錄后才能下訂單哦!
這篇文章主要介紹“怎么用C#實現接收QQ郵件功能”的相關知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“怎么用C#實現接收QQ郵件功能”文章能幫助大家解決問題。
C#獲取QQ郵箱內的郵件
開發工具: Visual Studio 2013
.NET Framework版本:4.5
static class Program { static string mail = "", pwd = ""; static void Main(string[] args) { Connect(); Console.WriteLine("結束運行"); Console.ReadKey(); } static void Connect() { TcpClient tcpClient = new TcpClient("pop.qq.com", 995); Console.WriteLine("已建立連接"); SslStream sslStream = new SslStream(tcpClient.GetStream(), true); sslStream.AuthenticateAsClient("pop.qq.com"); //發送郵箱賬號 sslStream.SendPop("user " + mail); Console.WriteLine(sslStream.ReadString().msg); //發送郵箱密碼 sslStream.SendPop("pass " + pwd); Console.WriteLine(sslStream.ReadString().msg); //獲取郵箱統計數據 sslStream.SendPop("stat"); Console.WriteLine(sslStream.ReadString().msg); //獲取郵件數量和每個郵件的大小 sslStream.SendPop("list"); string listMsg = sslStream.ReadString().msg; List<string> list = listMsg.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries).ToList(); if (list.Count < 2) { Console.WriteLine("未獲取到郵件"); return; } list.RemoveAt(0); list.RemoveAt(list.Count - 1); for (int i = list.Count - 1; i > -1; i--) { string[] arr = list[i].Split(' '); //獲取郵件前n行內容 sslStream.SendPop("top " + arr[0] + " 1"); Console.WriteLine(list[i]); Console.WriteLine(GetHeader(sslStream.ReadString().msg)); Console.WriteLine(); Console.WriteLine(); } //string id = Console.ReadLine(); //sslStream.SendPop("retr " + id); //Console.WriteLine(GetContext(sslStream.ReadString().msg)); sslStream.Close(); tcpClient.Close(); } /// <summary> /// 解析郵件標題 /// </summary> /// <param name="text"></param> /// <returns></returns> static string GetHeader(string text) { List<string> list = text.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries).ToList(); list.RemoveAt(0); list.RemoveAt(list.Count - 1); StringBuilder sb = new StringBuilder(); for (int i = 1; i < list.Count; i++) { if (list[i].StartsWith("Date: ")) { sb.AppendLine("發送時間:" + list[i].Substring("Date: ".Length)); } if (list[i].StartsWith("From: ")) { string value = list[i].Substring("From: ".Length); sb.AppendLine("發送人:" + GetText(value)); } if (list[i].StartsWith("Subject: ")) { string value = list[i].Substring("Subject: ".Length); sb.AppendLine("郵件標題:" + GetText(value)); } if (list[i].StartsWith("Cc: ")) { string value = list[i].Substring("Cc: ".Length); sb.AppendLine("抄送:" + GetText(value)); } } return sb.ToString(); } /// <summary> /// 解析郵件內容 /// </summary> /// <param name="text"></param> /// <returns></returns> static string GetContext(string text) { List<string> list = text.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries).ToList(); list.RemoveAt(0); list.RemoveAt(list.Count - 1); StringBuilder sb = new StringBuilder(); string s = list.Where(a => a.StartsWith("Content-Transfer-Encoding:")).SingleOrDefault(); int index = list.IndexOf(s) + 1; for (int i = index; i < list.Count - index; i++) { sb.AppendLine(GetText(list[i])); } return sb.ToString(); } /// <summary> /// 解析原始文本 /// </summary> /// <param name="value"></param> /// <returns></returns> static string GetText(string value) { string encoding = "utf-8"; if (value.IndexOf("=?") == 0) { string tag = ""; if (value.Contains("?B?")) { tag = "?B?"; encoding = value.SubStr("=?", tag); } if (value.Contains("?b?")) { tag = "?b?"; encoding = value.SubStr("=?", tag); } if (value.Contains("?Q?")) { tag = "?Q?"; encoding = value.SubStr("=?", tag); } if (tag != "") { string last = ""; int lastIndex = value.LastIndexOf("?="); if (lastIndex != value.TrimEnd().Length - 2) { last = value.Substring(lastIndex + 2); } string text = value.SubStr(tag, "?="); if (tag.ToLower().Contains("b")) { return Encoding.GetEncoding(encoding).GetString(Convert.FromBase64String(text)) + last; } else if (tag.ToLower().Contains("q")) { return DecodeQP(text, encoding) + last; } } } return value; } static string SubStr(this string text, string start, string end) { try { int s = text.IndexOf(start); int e = text.LastIndexOf(end); if (s == -1 || e == -1) { return text; } string result = text.Substring(s + start.Length, e - s - start.Length); return result; } catch (Exception ex) { throw ex; } } /// <summary> /// 發送數據到pop /// </summary> /// <param name="sslStream"></param> /// <param name="text"></param> /// <param name="isNewLine"></param> static void SendPop(this SslStream sslStream, string text, bool isNewLine = true) { if (isNewLine) { text += "\r\n"; } sslStream.Write(Encoding.ASCII.GetBytes(text)); } static dynamic ReadString(this SslStream sslStream) { try { byte[] buffer = new byte[2048 * 2048]; int len = sslStream.Read(buffer, 0, buffer.Length); string result = Encoding.UTF8.GetString(buffer, 0, len); if (result.StartsWith("-ERR")) { throw new Exception(result); } else { return new { code = 1, msg = result }; } } catch (Exception ex) { return new { code = 0, msg = "讀取錯誤:" + ex.Message }; } } //Quoted-Printable 解碼(QP解碼) static string DecodeQP(string data, string encoding) { char ch; string ret = ""; byte[] bytes = new byte[data.Length]; int bIdex = 0; try { for (int rIndex = 0; rIndex < data.Length; rIndex++) { ch = data[rIndex]; if (ch == '=') { rIndex++; if (rIndex < data.Length && (Char.IsDigit(data[rIndex]) || Char.IsLetter(data[rIndex]))) { bytes[bIdex++] = Byte.Parse(data.Substring(rIndex++, 2), System.Globalization.NumberStyles.HexNumber); continue; } if (rIndex < data.Length && data[rIndex] == '\r' && (rIndex + 1) < data.Length && data[rIndex + 1] == '\n') { rIndex++; continue; } } if (ch == '\r' || ch == '\n') continue; bytes[bIdex++] = (byte)ch; } ret = Encoding.GetEncoding(encoding).GetString(bytes, 0, bIdex); } catch (Exception ex) { Console.WriteLine(ex.Message); } return ret; } }
關于“怎么用C#實現接收QQ郵件功能”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識,可以關注億速云行業資訊頻道,小編每天都會為大家更新不同的知識點。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。