[Mono-dev] Trying to find a bug in my code...

pfj pjohnson1 at uclan.ac.uk
Wed Feb 24 04:57:43 EST 2010


Hi,

I have a very simple application which is connecting happily to gmail using
SSL. The problem is that it dies when I come to grabbing a list of the
emails on the server.

My connection and retrieval code is this

My code looks like this (pop3 first)

// from pop3.cs

using System;
using System.Collections;
using System.Net.Sockets;
using System.Net.Security;

namespace email_to_paul_winform
{
        /// <summary>
        /// Description of pop3.
        /// </summary>
        public class pop3
        {
                public pop3()
                {
                }
               
                public class Pop3Exception : System. ApplicationException
                {
    public Pop3Exception( string str) : base( str)
    {
    }
                }
       
                public class Pop3Message
                {
    public long number;
    public long bytes;
    public bool retrieved;
    public string message;
                }
               
                SslStream netstream;
               
                public void Connect(string server, string username, string
password)
                {
    string message;
    string response;
    TcpClient tcpClient = new TcpClient();
    tcpClient.Connect(server, 995);
    netstream = new SslStream(tcpClient.GetStream());
    netstream.AuthenticateAsClient("pop.gmail.com");
    response = Response(netstream);
    if (response.Substring(0, 3) != "+OK")
    {
        throw new Pop3Exception(response);
    }

    message = "USER " + username + "\r\n";
    Write(message, netstream);
    response = Response(netstream);
    if (response.Substring(0, 3) != "+OK")
    {
        throw new Pop3Exception(response);
    }

    message = "PASS " + password + "\r\n";
    Write(message, netstream);
    response = Response(netstream);
    if (response.Substring(0, 3) != "+OK")
    {
        throw new Pop3Exception(response);
    }
                }
               
                public void Disconnect()
                {
    string message;
    string response;
    message = "QUIT\r\n";
    Write(message, netstream);
    response = Response(netstream);
    if (response.Substring(0, 3) != "+OK")
    {
        throw new Pop3Exception(response);
    }
                }
               
                public ArrayList List()
                {
    string message;
    string response;

    ArrayList retval = new ArrayList();
    message = "LIST\r\n";
    Write(message, netstream);
    response = Response(netstream);
    if (response.Substring(0, 3) != "+OK")
    {
       throw new Pop3Exception(response);
    }

    while (true)
    {
        response = Response(netstream);
        if (response == ".\r\n")
        {
           return retval;
        }
        else
        {
            Pop3Message msg = new Pop3Message();
            char[] seps = { ' ' };
            string[] values = response.Split(seps);
            msg.number = Int32.Parse(values[0]);
            msg.bytes = Int32.Parse(values[1]);
            msg.retrieved = false;
            retval.Add(msg);
            continue;
        }
    }
                }
               
                public Pop3Message Retrieve(Pop3Message rhs)
                {
    string message;
    string response;

    Pop3Message msg = new Pop3Message();
    msg.bytes = rhs.bytes;
    msg.number = rhs.number;

    message = "RETR " + rhs.number + "\r\n";
    Write(message, netstream);
    response = Response(netstream);
    if (response.Substring(0, 3) != "+OK")
    {
        throw new Pop3Exception(response);
    }

    msg.retrieved = true;
    while (true)
    {
        response = Response(netstream);
        if (response == ".\r\n")
        {
            break;
        }
        else
        {
            msg.message += response;
        }
    }

    return msg;
                }
               
                public void Delete(Pop3Message rhs)
                {
    string message;
    string response;

    message = "DELE " + rhs.number + "\r\n";
    Write(message, netstream);
    response = Response(netstream);
    if (response.Substring(0, 3) != "+OK")
    {
        throw new Pop3Exception(response);
    }
                }
               
                private void Write(string message, SslStream stream)
                {
    System.Text.ASCIIEncoding en = new System.Text.ASCIIEncoding() ;

    byte[] WriteBuffer = new byte[1024] ;
    WriteBuffer = en.GetBytes(message) ;
                       
    //TcpClient tcpClient = new TcpClient();
   
    //NetworkStream stream = tcpClient.GetStream() ;
    stream.Write(WriteBuffer, 0, WriteBuffer.Length);

    //Debug.WriteLine("WRITE:" + message);
                }
               
                private string Response(SslStream stream)
                {
    System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
    byte[] serverbuff = new Byte[1024];
    //TcpClient tcpClient = new TcpClient();
   
    //NetworkStream stream = tcpClient.GetStream();
    int count = 0;
    while (true)
    {
        byte[] buff = new Byte[2];
        int bytes = stream.Read(buff, 0, 1 );
        if (bytes == 1)
        {
            serverbuff[count] = buff[0];
            count++;

            if (buff[0] == '\n')
            {
                break;
            }
        }
        else
        {
            break;
        };
    };

    string retval = enc.GetString(serverbuff, 0, count );
    //Debug.WriteLine("READ:" + retval);
    return retval;
                }
        }
}

The driving code looks like this

                private void connect()
                {
                        pop3 pop = new pop3();
                        try
                        {
                                pop.Connect("pop.gmail.com",
"valid_username", "valid_password");
                                ArrayList list = pop.List();
                                foreach (pop3.Pop3Message message in list)
                                {
                                        pop3.Pop3Message msg2 =
pop.Retrieve(message);
                                        emails.Items.Add(msg2.ToString());
                                }
                                pop.Disconnect();
                        }
                        catch (pop3.Pop3Exception e )
    {
                                MessageBox.Show(e.ToString(), "Error with
connection",
MessageBoxButtons.OK);
    }
    catch (System.Exception e)
    {
        MessageBox.Show(e.ToString(), "General error",
MessageBoxButtons.OK);
    } 

When I run this through the debugger, everything seems to be working, except
if I look at what is being retrieved in the List method. Here the messages
don't look right (it's giving me things like response = 1 4935). I don't
know if this needs decrypting (as the connection uses SSL) or that because
netstream is instantated in the Connect() method, as soon as the Connect()
is done, the instantation is disposed off, therefore what is being seen in
List is actually undefined behaviour.

Can anyone shed any light on this?

TTFN

Paul


-- 
View this message in context: http://n4.nabble.com/Trying-to-find-a-bug-in-my-code-tp1567215p1567215.html
Sent from the Mono - Dev mailing list archive at Nabble.com.


More information about the Mono-devel-list mailing list