[Mono-list] Grabbing a list of emails from google.
Paul
paul at all-the-johnsons.co.uk
Tue Feb 23 02:32:51 EST 2010
Hi,
I have a really simple POP3 implementation which uses SSL so that I can
log into googlemail. However, the code hangs if I try to get a list of
the emails.
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);
}
Everything connects and the debugger says everything is being retrieved
ok. The problem looks to be in the foreach section as it looks like this
is where it hangs.
Any ideas?
TTFN
Paul
--
Sie können mich aufreizen und wirklich heiß machen!
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: This is a digitally signed message part
Url : http://lists.ximian.com/pipermail/mono-list/attachments/20100223/e704944a/attachment-0001.bin
More information about the Mono-list
mailing list