views:

139

answers:

3

Hy,

I have an application that sends mails with Gmail SMTP server (smtp.gmail.com) using SSL.

Now I want to read the emails from that account, does anyone have any idea how can I make this programatically in C# and ASP.NET?

At this point I'm using this code:

TcpClient tcpClient = new TcpClient();
tcpClient.Connect("pop.gmail.com", 587);

NetworkStream netStream = tcpClient.GetStream();
System.IO.StreamReader strReader = new System.IO.StreamReader(netStream);

Label7.Text = strReader.ReadLine() + "<br />"; 
//Label7.Text = "Server connected!";

byte[] WriteBuffer = new byte[1024];
ASCIIEncoding enc = new System.Text.ASCIIEncoding();

WriteBuffer = enc.GetBytes("USER " + TextBox4.Text + "\r\n");
netStream.Write(WriteBuffer, 0, WriteBuffer.Length);
Label7.Text += strReader.ReadLine() + "<br />";

WriteBuffer = enc.GetBytes("PASS " + TextBox5.Text + "\r\n");
netStream.Write(WriteBuffer, 0, WriteBuffer.Length);
Label7.Text += strReader.ReadLine() + "<br />";

WriteBuffer = enc.GetBytes("LIST\r\n");
netStream.Write(WriteBuffer, 0, WriteBuffer.Length);

String ListMessage;
while (true)
{
    ListMessage = strReader.ReadLine();
    if (ListMessage == ".")
    {
        break;
    }
    else
    {
        Label7.Text += ListMessage + "<br />";
        continue;
    }
}

WriteBuffer = enc.GetBytes("QUIT\r\n");
netStream.Write(WriteBuffer, 0, WriteBuffer.Length);
Label7.Text += strReader.ReadLine() + "<br />"; 

And when I debug it it's shows that it's connected but no response in retrieving emails.

A: 

I´m using this sample library that appeared at The Code Project link text that has a nice and clean api to work with pop3.

iCe
+1  A: 

POP3 features of this open source project (I'm involved in) contains everything you need. Including secure communications support & advanced authentication.

If you really want to do one yourself, browsing the source code will probably save you days of development.

Pierre 303
and this is working with any kind of account? Like a gmail account?
Jeff Norman
Any POP3 account, including gmail.
Pierre 303
A: 

@Pierre 303

Thanks for the refference.

I have this code now:

        Pop3Client pop = new Pop3Client();
        try
        {
            Label7.Text = string.Format("Connection to the pop 3 server : {0}", "pop.gmail.com");
            pop.ConnectSsl("pop.gmail.com", 995, TextBox4.Text, TextBox5.Text);
            Label7.Text += string.Format("Message Count: {0}", pop.MessageCount.ToString());
            if (pop.MessageCount > 0)
            {
                Header msgHeader = pop.RetrieveHeaderObject(1);

                Label7.Text += string.Format("Subject: {0} From :{1} Date Sent {2}", msgHeader.Subject, msgHeader.From.Email, msgHeader.DateString);
            }
        }

        catch (Pop3Exception pexp)
        {
            Label7.Text = string.Format("Pop3 Error: {0}", pexp.Message);
        }

        catch (Exception ex)
        {
            Label7.Text = string.Format("Failed: {0}", ex.Message);
        }

        finally
        {
            if (pop.IsConnected)
            {
                pop.Disconnect();
            }
        }

I'm not getting any error but the message count it's 0... and I have probably 100 mails in my inbox...

I'm missing something?

Jeff Norman
Post your issue on the open source project's forum or issue tracker. Stack Overflow is not a support board. At least not yet ;)
Pierre 303
oky i will post my issue there ;)
Jeff Norman
@Pierre I've posted the issue where you told me... but with no response... can you please tell me what am I missing?
Jeff Norman