views:

90

answers:

1

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.

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.

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 [email protected] 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);
        }
+1  A: 

You might look at using a canned IMAP/SSL library instead - there is one that is still active here.

This alternative is not free.

The basis for one of these has source code that might be helpful since you want to roll your own protocol handler.

Steve Townsend
I already knew those two but my main objective was to learn how to communicate with a IMAP server...
Miguel Ribeiro
@Miguel - thanks for the clarification, see edit
Steve Townsend