Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have this code, but I do not know how I can get number of unread emails.
C#
private void button1_Click(object sender, EventArgs e)
{
    TcpClient mail = new TcpClient();
    SslStream sslStream;
    int bytes = -1;
    Cursor = Cursors.WaitCursor;


    mail.Connect("pop.gmail.com", 995);
    sslStream = new SslStream(mail.GetStream());

    sslStream.AuthenticateAsClient("pop.gmail.com");
    Cursor = Cursors.Default;
    byte[] buffer = new byte[2048];

    // Read the stream to make sure we are connected
    bytes = sslStream.Read(buffer, 0, buffer.Length);

    sslStream.Write(Encoding.ASCII.GetBytes("USER USER_EMAIL"+ "\r\n"));
    sslStream.Flush();
    bytes = sslStream.Read(buffer, 0, buffer.Length);
    listBox1.Items.Add(Encoding.ASCII.GetString(buffer, 0, bytes));

    sslStream.Write(Encoding.ASCII.GetBytes("PASS USER_PASSWORD" + "\r\n"));
    sslStream.Flush();
    bytes = sslStream.Read(buffer, 0, buffer.Length);
    listBox1.Items.Add(Encoding.ASCII.GetString(buffer, 0, bytes));

    sslStream.Write(Encoding.ASCII.GetBytes("STAT\r\n"));
    sslStream.Flush();
    bytes = sslStream.Read(buffer, 0, buffer.Length);
    listBox1.Items.Add(Encoding.ASCII.GetString(buffer, 0, bytes));

    sslStream.Write(Encoding.ASCII.GetBytes("RETR 1\r\n"));
    bytes = sslStream.Read(buffer, 0, buffer.Length);
    listBox1.Items.Add(Encoding.ASCII.GetString(buffer, 0, bytes));
    mail.Close();
}
Posted
Updated 26-Mar-15 22:02pm
v2
Comments
Afzaal Ahmad Zeeshan 27-Mar-15 5:08am    
Unread emails are having a flag Seen equal to false. So look for those emails.
aazam rafiee zade 27-Mar-15 5:20am    
what code do I add to my program to get just the number of emails? If I know just the number of emails, then I can compare thoese. Thank you.
aazam rafiee zade 27-Mar-15 9:40am    
I used this link: http://p2p.wrox.com/net-framework-2-0/70999-how-get-message-headers-pop3-server.html. I add this code: string[] BreakDown = messageData.Split(" ".ToCharArray()); int messagecount = Convert.ToInt16(BreakDown[1]);. Now I have an integer number, but this number is not correct.
aazam rafiee zade 28-Mar-15 12:08pm    
I used difference of the NEW and OLD messagecount for solving my question.
aazam rafiee zade 29-Mar-15 11:38am    
These two links are also useful: http://stackoverflow.com/questions/7026101/imap-encoded-emails and https://kevinthant.wordpress.com/2009/07/11/coding-imap-email-access-using-asp-netc/

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900