qq邮箱怎么写 填写正确qq邮箱格式怎么写

时间:2023-05-14 20:23/span> 作者:tiger 分类: 新知 浏览:7062 评论:0

前面写了一篇通过smtp协议利用qq邮箱去实现发送邮件的功能。这一篇我们使用pop协议来实现一下接收邮件。

由于邮件的内容类型比较丰富,我暂时没有一个比较好的解决方案(确切地说我是懒得一种种去解析),所以这里就获取下邮件的列表,以及邮件的标题信息

实现功能:

    • C获取QQ邮箱内的邮件

开发环境:

开发工具: Visual Studio 2013

.NET Framework版本:4.5

实现代码:

 static class Program
    {

        static string mail = &34;&34;, pwd = &34;&34;;
        static void Main(string[] args)
        {
            Connect();
            Console.WriteLine(&34;结束运行&34;);
            Console.ReadKey();
        }
     
          static void Connect()
        {

            TcpClient tcpClient = new TcpClient(&34;pop.qq.com&34;, 995);
            Console.WriteLine(&34;已建立连接&34;);

            SslStream sslStream = new SslStream(tcpClient.GetStream(), true);
            sslStream.AuthenticateAsClient(&34;pop.qq.com&34;);

            //发送邮箱账号
            sslStream.SendPop(&34;user &34; + mail);
            Console.WriteLine(sslStream.ReadString().msg);

            //发送邮箱密码
            sslStream.SendPop(&34;pass &34; + pwd);
            Console.WriteLine(sslStream.ReadString().msg);

            //获取邮箱统计数据
            sslStream.SendPop(&34;stat&34;);
            Console.WriteLine(sslStream.ReadString().msg);

            //获取邮件数量和每个邮件的大小
            sslStream.SendPop(&34;list&34;);
            string listMsg = sslStream.ReadString().msg;
            List<string> list = listMsg.Split(new string[] { &34;\r\n&34; }, StringSplitOptions.RemoveEmptyEntries).ToList();
            if (list.Count < 2)
            {
                Console.WriteLine(&34;未获取到邮件&34;);
                return;
            }
            list.RemoveAt(0);
            list.RemoveAt(list.Count - 1);
            for (int i = list.Count - 1; i > -1; i--)
            {
                string[] arr = list[i].Split(&39; &39;);
                //获取邮件前n行内容
                sslStream.SendPop(&34;top &34; + arr[0] + &34; 1&34;);
                Console.WriteLine(list[i]);
                Console.WriteLine(GetHeader(sslStream.ReadString().msg));
                Console.WriteLine();
                Console.WriteLine();
            }

            //string id = Console.ReadLine();

            //sslStream.SendPop(&34;retr &34; + id);
            //Console.WriteLine(GetContext(sslStream.ReadString().msg));

            sslStream.Close();
            tcpClient.Close();
        }

        /// <summary>
        /// 解析邮件标题
        /// </summary>
        /// <param name=&34;text&34;></param>
        /// <returns></returns>
        static string GetHeader(string text)
        {
            List<string> list = text.Split(new string[] { &34;\r\n&34; }, 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(&34;Date: &34;))
                {
                    sb.AppendLine(&34;发送时间:&34; + list[i].Substring(&34;Date: &34;.Length));
                }
                if (list[i].StartsWith(&34;From: &34;))
                {
                    string value = list[i].Substring(&34;From: &34;.Length);
                    sb.AppendLine(&34;发送人:&34; + GetText(value));
                }
                if (list[i].StartsWith(&34;Subject: &34;))
                {
                    string value = list[i].Substring(&34;Subject: &34;.Length);
                    sb.AppendLine(&34;邮件标题:&34; + GetText(value));
                }
                if (list[i].StartsWith(&34;Cc: &34;))
                {
                    string value = list[i].Substring(&34;Cc: &34;.Length);
                    sb.AppendLine(&34;抄送:&34; + GetText(value));
                }

            }
            return sb.ToString();
        }

        /// <summary>
        /// 解析邮件内容
        /// </summary>
        /// <param name=&34;text&34;></param>
        /// <returns></returns>
        static string GetContext(string text)
        {
            List<string> list = text.Split(new string[] { &34;\r\n&34; }, StringSplitOptions.RemoveEmptyEntries).ToList();
            list.RemoveAt(0);
            list.RemoveAt(list.Count - 1);
            StringBuilder sb = new StringBuilder();
            string s = list.Where(a => a.StartsWith(&34;Content-Transfer-Encoding:&34;)).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=&34;value&34;></param>
        /// <returns></returns>
        static string GetText(string value)
        {
            string encoding = &34;utf-8&34;;
            if (value.IndexOf(&34;=?&34;) == 0)
            {
                string tag = &34;&34;;
                if (value.Contains(&34;?B?&34;))
                {
                    tag = &34;?B?&34;;
                    encoding = value.SubStr(&34;=?&34;, tag);
                }
                if (value.Contains(&34;?b?&34;))
                {
                    tag = &34;?b?&34;;
                    encoding = value.SubStr(&34;=?&34;, tag);
                }
                if (value.Contains(&34;?Q?&34;))
                {
                    tag = &34;?Q?&34;;
                    encoding = value.SubStr(&34;=?&34;, tag);
                }
                if (tag != &34;&34;)
                {
                    string last = &34;&34;;
                    int lastIndex = value.LastIndexOf(&34;?=&34;);
                    if (lastIndex != value.TrimEnd().Length - 2)
                    {
                        last = value.Substring(lastIndex + 2);
                    }
                    string text = value.SubStr(tag, &34;?=&34;);
                    if (tag.ToLower().Contains(&34;b&34;))
                    {
                        return Encoding.GetEncoding(encoding).GetString(Convert.FromBase64String(text)) + last;
                    }
                    else if (tag.ToLower().Contains(&34;q&34;))
                    {
                        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=&34;sslStream&34;></param>
        /// <param name=&34;text&34;></param>
        /// <param name=&34;isNewLine&34;></param>
        static void SendPop(this SslStream sslStream, string text, bool isNewLine = true)
        {
            if (isNewLine)
            {
                text += &34;\r\n&34;;
            }
            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(&34;-ERR&34;))
                {
                    throw new Exception(result);
                }
                else
                {
                    return new { code = 1, msg = result };
                }
            }
            catch (Exception ex)
            {
                return new { code = 0, msg = &34;读取错误:&34; + ex.Message };
            }
        }

        //Quoted-Printable 解码(QP解码)
        static string DecodeQP(string data, string encoding)
        {
            char ch;
            string ret = &34;&34;;
            byte[] bytes = new byte[data.Length];
            int bIdex = 0;

            try
            {
                for (int rIndex = 0; rIndex < data.Length; rIndex++)
                {
                    ch = data[rIndex];
                    if (ch == &39;=&39;)
                    {
                        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] == &39;\r&39; && (rIndex + 1) < data.Length && data[rIndex + 1] == &39;\n&39;)
                        {
                            rIndex++;
                            continue;
                        }
                    }
                    if (ch == &39;\r&39; || ch == &39;\n&39;)
                        continue;
                    bytes[bIdex++] = (byte)ch;
                }
                ret = Encoding.GetEncoding(encoding).GetString(bytes, 0, bIdex);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return ret;
        }
    }

由简入繁,拿来即用

后续精彩,持续关注

文章评论