How to use SSL in TcpClient class

后端 未结 3 2049
庸人自扰
庸人自扰 2021-01-30 23:46

In the .NET framework there is a class TcpClient to retrieve emails from an email server. The TcpClient class has 4 constructors to connect with the se

3条回答
  •  情书的邮戳
    2021-01-31 00:09

    You have to use the SslStream along with the TcpClient then use the SslStream to read the data rather than the TcpClient.

    Something along the lines of:

            TcpClient mail = new TcpClient();
            SslStream sslStream;
    
            mail.Connect("pop.gmail.com", 995);
            sslStream = new SslStream(mail.GetStream());
    
            sslStream.AuthenticateAsClient("pop.gmail.com");
    
            byte[] buffer = new byte[2048];
            StringBuilder messageData = new StringBuilder();
            int bytes = -1;
            do
            {
                bytes = sslStream.Read(buffer, 0, buffer.Length);
    
                Decoder decoder = Encoding.UTF8.GetDecoder();
                char[] chars = new char[decoder.GetCharCount(buffer, 0, bytes)];
                decoder.GetChars(buffer, 0, bytes, chars, 0);
                messageData.Append(chars);
    
                if (messageData.ToString().IndexOf("") != -1)
                {
                    break;
                }
            } while (bytes != 0);
    
            Console.Write(messageData.ToString());
            Console.ReadKey();
    

    EDIT

    The above code will simply connect via SSL to Gmail and output the contents of a test message. To log in to a gmail account and issue commands you will need to do something along the lines of:

            TcpClient mail = new TcpClient();
            SslStream sslStream;
            int bytes = -1;
    
            mail.Connect("pop.gmail.com", 995);
            sslStream = new SslStream(mail.GetStream());
    
            sslStream.AuthenticateAsClient("pop.gmail.com");
    
            byte[] buffer = new byte[2048];
            // Read the stream to make sure we are connected
            bytes = sslStream.Read(buffer, 0, buffer.Length);
            Console.WriteLine(Encoding.ASCII.GetString(buffer, 0, bytes));
    
            //Send the users login details
            sslStream.Write(Encoding.ASCII.GetBytes("USER USER_EMAIL\r\n"));
            bytes = sslStream.Read(buffer, 0, buffer.Length);
            Console.WriteLine(Encoding.ASCII.GetString(buffer, 0, bytes));
    
            //Send the password                        
            sslStream.Write(Encoding.ASCII.GetBytes("PASS USER_PASSWORD\r\n"));
            bytes = sslStream.Read(buffer, 0, buffer.Length);
            Console.WriteLine(Encoding.ASCII.GetString(buffer, 0, bytes));
    
            // Get the first email 
            sslStream.Write(Encoding.ASCII.GetBytes("RETR 1\r\n"));
            bytes = sslStream.Read(buffer, 0, buffer.Length);
            Console.WriteLine(Encoding.ASCII.GetString(buffer, 0, bytes));
    

    Obviously, without all the duplication of code :)

提交回复
热议问题