How to use SSL in TcpClient class

后端 未结 3 1974
庸人自扰
庸人自扰 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:05

    To tailor the best response above specifically to the question of IMAP and IMAP authentication, you will need to modify the code slightly to use IMAP commands as follows. For debugging, you can set breakpoints just after strOut is assigned to view the server responses.

                pmOffice pmO = new pmOffice();
                pmO.GetpmOffice(3, false);
    
                TcpClient mail = new TcpClient();
                SslStream sslStream;
                int bytes = -1;
    
                mail.Connect("outlook.office365.com", 993);
                sslStream = new SslStream(mail.GetStream());
    
                sslStream.AuthenticateAsClient("outlook.office365.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 (insert your username & password in the following line
                sslStream.Write(Encoding.ASCII.GetBytes("$ LOGIN " + pmO.mailUsername + " " + pmO.mailPassword + "\r\n"));
                bytes = sslStream.Read(buffer, 0, buffer.Length);
                string strOut = Encoding.ASCII.GetString(buffer, 0, bytes);
    
                // Get the status of the inbox (# of messages)
                sslStream.Write(Encoding.ASCII.GetBytes("$ STATUS INBOX (messages)\r\n"));
                bytes = sslStream.Read(buffer, 0, buffer.Length);
                strOut = Encoding.ASCII.GetString(buffer, 0, bytes);
    

提交回复
热议问题