Send a suggestion!

We're building a brand new version of the site, and we'd love to hear your ideas

Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 123,968 times

Contents

Related Categories

How to POP3 in C# - Supporting Functions

randy

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.

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

Comments

  • Re: [4071] How to POP3 in C#

    Posted by mrymyousefii on 30 Jul 2008

    I got codes for receiving email from one of the article and made some necessary adjustments, but giving me this exception:


     


    System.Net.Sockets.SocketException...

  • Re: [4071] How to POP3 in C#

    Posted by burnt1ce85 on 12 Jun 2008

    Most pop3 mail servers encrypt their connection using SSL (port: 995). This tutorial seems to only cover accessing email using an unecrypted connection. Anyone knows a tutorial that covers encrypte...

  • Re: C#.net coding regards

    Posted by seamnia on 12 Nov 2007

    Hi VidyaRe: [4071] How to POP3 in C#

    Posted by thilagam on 20 Aug 2007

    Thanks for publishing this wonderfull article. I want to know how to save the attachment to the specified location automatically

  • mail.dll

    Posted by lesnikowski on 10 Nov 2005

    [url="http://www.lesnikowski.com/mail"]http://www.lesnikowski.com/mail[/url]