title: Send IMAP commands to GMail using C#
body: <p>I've been trying to access my GMail account to retrieve the unread emails from my email account. However, I only con perform login... Anything after that doesn't work.</p>
  
  <p>First of all I connect to the server, then send the login command and finally the examine command. The thing is that the responses that are receive refers only to the connection and to the login. After that, it just stops waiting for someting to read from the StreamReader.</p>
  
  <pre><code>try
          {
              // create an instance of TcpClient
              TcpClient tcpclient = new TcpClient();
              // HOST NAME POP SERVER and gmail uses port number 995 for POP  
              //tcpclient.Connect("pop.gmail.com", 995);
              tcpclient.Connect("imap.gmail.com", 993);
              // This is Secure Stream // opened the connection between client and POP Server
              SslStream sslstream = new SslStream(tcpclient.GetStream());
              // authenticate as client  
  
              sslstream.AuthenticateAsClient("imap.gmail.com");
  
              bool flag = sslstream.IsAuthenticated;   // check flag
              // Asssigned the writer to stream
              System.IO.StreamWriter sw = new StreamWriter(sslstream);
              // Assigned reader to stream
              System.IO.StreamReader reader = new StreamReader(sslstream);
  
              sw.WriteLine("tag LOGIN user@gmail.com pass");
              sw.Flush();
  
              sw.WriteLine("tag2 EXAMINE inbox");
              sw.Flush();
  
              sw.WriteLine("tag3 LOGOUT ");
              sw.Flush();
  
              string str = string.Empty;
              string strTemp = string.Empty;
              try
              {
                  while ((strTemp = reader.ReadLine()) != null)
                  {
                      Console.WriteLine(strTemp);
                      // find the . character in line
                      if (strTemp == ".")
                      {
                          //reader.Close();
                          break;
                      }
                      if (strTemp.IndexOf("-ERR") != -1)
                      {
                          //reader.Close();
                          break;
                      }
                      str += strTemp;
                  }
              }
              catch (Exception ex)
              {
                  string s = ex.Message;
              }
              //reader.Close();
  
          }
          catch (Exception ex)
          {
              Console.WriteLine(ex.Message);
          }
  </code></pre>