91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么用C#實現接收QQ郵件功能

發布時間:2022-08-31 10:56:23 來源:億速云 閱讀:127 作者:iii 欄目:開發技術

這篇文章主要介紹“怎么用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郵件功能”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識,可以關注億速云行業資訊頻道,小編每天都會為大家更新不同的知識點。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

孟连| 乌苏市| 藁城市| 望城县| 大理市| 陆河县| 竹山县| 沧州市| 许昌市| 巴林左旗| 乃东县| 新丰县| 彰武县| 且末县| 三明市| 鹤庆县| 阿拉善右旗| 琼海市| 米林县| 胶南市| 汤阴县| 新邵县| 武冈市| 始兴县| 永兴县| 宜君县| 徐闻县| 湖州市| 长宁区| 鄯善县| 遂宁市| 鄱阳县| 阳高县| 久治县| 莲花县| 德惠市| 昭觉县| 定远县| 新平| 上犹县| 白玉县|