<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
前面寫了一篇通過smtp協定利用qq郵箱去實現傳送郵件的功能。這一篇我們使用pop協定來實現一下接收郵件。
由於郵件的內容型別比較豐富,我暫時沒有一個比較好的解決方案(確切的說我是懶得一種種去解析),所以這裡就獲取下郵件的列表,以及郵件的標題資訊
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[] { "rn" }, 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[] { "rn" }, 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[] { "rn" }, 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 += "rn"; } 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郵件的範例程式碼的文章就介紹到這了,更多相關C#接收QQ郵件內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45