繁体   English   中英

接收来自gmail的消息

[英]Receive messages from gmail

我尝试从gmail帐户接收消息,但收到错误消息: host not found

这是我的代码:

        using (TcpClient client = new TcpClient("pop.gmail.com ", 995))
        using (NetworkStream n = client.GetStream())
        {
            ReadLine(n);                             // Read the welcome message.
            SendCommand(n, "my_login@gmail.com");
            SendCommand(n, "my_password");
            SendCommand(n, "LIST");                  // Retrieve message IDs
            List<int> messageIDs = new List<int>();
            while (true)
            {
                string line = ReadLine(n);             // e.g.  "1 1876"
                if (line == ".") break;
                messageIDs.Add(int.Parse(line.Split(' ')[0]));   // Message ID
            }

            foreach (int id in messageIDs)         // Retrieve each message.
            {
                SendCommand(n, "RETR " + id);
                string randomFile = Guid.NewGuid().ToString() + ".eml";
                using (StreamWriter writer = File.CreateText(randomFile))
                    while (true)
                    {
                        string line = ReadLine(n);      // Read next line of message.
                        if (line == ".") break;          // Single dot = end of message.
                        if (line == "..") line = ".";    // "Escape out" double dot.
                        writer.WriteLine(line);         // Write to output file.
                    }
                SendCommand(n, "DELE " + id);       // Delete message off server.
            }
            SendCommand(n, "QUIT");
        }

       static void SendCommand(Stream stream, string line)
       {
           byte[] data = Encoding.UTF8.GetBytes(line + "\r\n");
           stream.Write(data, 0, data.Length);
           string response = ReadLine(stream);
           if (!response.StartsWith("+OK"))
               throw new Exception("POP Error: " + response);
       }

我的错误在哪里? 我也想从盒子里删除一些消息。 我怎样才能做到这一点?

非常感谢你!

我在您的代码中看到的第一个问题是它不使用SslStream(需要在端口995上连接到GMail的SSL封装的POP3服务)。

我看到的第二个问题是

if (line == "..") line = ".";

如果您实际上不愿意阅读POP3规范 ,则会发现以下几点

Responses to certain commands are multi-line. In these cases, which
are clearly indicated below, after sending the first line of the
response and a CRLF, any additional lines are sent, each terminated
by a CRLF pair. When all lines of the response have been sent, a
final line is sent, consisting of a termination octet (decimal code
046, ".") and a CRLF pair. If any line of the multi-line response
begins with the termination octet, the line is "byte-stuffed" by
pre-pending the termination octet to that line of the response.
Hence a multi-line response is terminated with the five octets
"CRLF.CRLF". When examining a multi-line response, the client checks
to see if the line begins with the termination octet. If so and if
octets other than CRLF follow, the first octet of the line (the
termination octet) is stripped away. If so and if CRLF immediately
follows the termination character, then the response from the POP
server is ended and the line containing ".CRLF" is not considered
part of the multi-line response.

这一点很重要: 在检查多行响应时,客户端检查行是否以终止八位位组开头。 如果是这样,并且紧跟着CRLF以外的八位位组,则删除该行的第一个八位位组(终止八位位组)。

这意味着如果客户端遇到诸如“ ..some text \\ r \\ n”之类的行,则以“。”开头。 需要被剥离。

仅当该行与“ .. \\ r \\ n”完全匹配时,您的代码才会将其删除。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM