Library tutorials & articles

How to POP3 in C#

Supporting Functions

The List, Retrieve and Delete methods used two private methods, Write and Response, to send and receive messages from the POP3 server.

private void Write(string message)
{
    System.Text.ASCIIEncoding en = new System.Text.ASCIIEncoding() ;

    byte[] WriteBuffer = new byte[1024] ;
    WriteBuffer = en.GetBytes(message) ;

    NetworkStream stream = GetStream() ;
    stream.Write(WriteBuffer, 0, WriteBuffer.Length);

    Debug.WriteLine("WRITE:" + message);
}

C# native strings, like Java native strings, are UNICODE. We therefore need to encode and decode the strings to and from ASCII. After encoding the string, we can then retrieve the socket stream by calling the TcpClient.GetStream method. I finish the Write method by called the Debug.Writeline method. This sends the string to the debug stream for help with debugging.

private string Response()
{
    System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
    byte[] serverbuff = new Byte[1024];
    NetworkStream stream = 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 Response method is similar to the Write method accept that we retrieve bytes from the stream before decoding them. Again we call the Debug.WriteLine method to send the read data to the debug stream and help with debugging. Note that we have a limitation in the Response method. We can only retrieve or send up to 1024 bytes at a time. I'll fix this in a later release. If you intend to use this in production, then you'll have to do the same first.

Comments

  1. 22 Jun 2009 at 14:22

    When i change the port to 587 from 110 i got the following error

    220 mx.google.com ESMTP g14sm11215098rvb.54

  2. 22 Jun 2009 at 14:17

    Hi Got the following error when i try to call

    obj.Connect("smtp.gmail.com", txtUser.Text, txtPass.Text);

    A Connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host failed to respond 209.85.199.111: 110

    Can any body please help me to fix this error?

  3. 01 Jun 2009 at 03:58

    Your code is too good. But in your code what happen If i send email today it display that email & tomarrow it show empty inbox. if i again send email it display that new mail only. And i also want to fetch Email subject,Email Body then what i do. Please give solution

  4. 19 Jan 2009 at 19:20
    **Image Hosted by ImageShack.us
    **
  5. 19 Jan 2009 at 19:20
    **I JUST GOT ERROR!!!!!! PLZ RESOLVE THIS ISSUE!!!! [URL=http://imageshack.us][IMG]http://img88.imageshack.us/img88/8177/errorsocketgy7.jpg[/IMG][/URL] [URL=http://g.imageshack.us/img88/errorsocketgy7.jpg/1/][IMG]http://img88.imageshack.us/img88/errorsocketgy7.jpg/1/w325.png[/IMG][/URL]**
  6. 19 Jan 2009 at 18:38
    ** TcpClient tcpClient=new TcpClient();**
  7. 19 Jan 2009 at 18:13
    **I don't know when this code was written but i do need to reference the TcpClient.Connect method! Guys who got confused like me do, TcpClient tcpClient=new TcpClien(); tcpClient.Connect();**
  8. 08 Dec 2008 at 10:22
    I t was cool needed quite alobarated to get the Mail.Body for specific Details... Thanks

Leave a comment

Sign in or Join us (it's free).

Randy Charles Morin Randy's article are Copyright 1998-2003 Randy Charles Morin
AddThis

Related discussion

Related podcasts

Want to stay in touch with what's going on? Follow us on twitter!