Library tutorials & articles

How to POP3 in C#

Retrieving Messages

Between the calls to Connect and Disconnect, the client may call three other methods, List, Retrieve and Delete, any number of times. The client will usually begin by calling our List method to retrieve an array of messages that are queued on the POP3 server.

public ArrayList List()
{
    string message;
    string response;

    ArrayList retval = new ArrayList();
    message = "LIST\r\n";
    Write(message);
    response = Response();
    if (response.Substring(0, 3) != "+OK")
    {
        throw new Pop3Exception(response);
    }

    while (true)
    {
        response = Response();
        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;
        }
    }
}

After sending the LIST message to the POP3 server, the server will respond with a +OK acknowledgement, followed by several lines representing one message each and finally by a line with a single period indicating the end of the messages. Each message line has two numbers, the first indicating the unique number of the message and the second indicating the message size in bytes.

Our List method will return a list of Pop3Message objects. The objects will only contain the message number and size of each message. In order to retrieve the full message, you can pass the message object to the Retrieve method. The Retrieve method will then respond with another Pop3Message containing the message content.

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);
    response = Response();
    if (response.Substring(0, 3) != "+OK")
    {
        throw new Pop3Exception(response);
    }

    msg.retrieved = true;
    while (true)
    {
        response = Response();
        if (response == ".\r\n")
        {
            break;
        }
        else
        {
            msg.message += response;
        }
    }

    return msg;
}

To retrieve a message from a POP3 server, we send a RETR message with the unique message number. The server then responds with the +OK acknowledgement, the message content and finally the single period terminating line.

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

We'd love to hear what you think! Submit ideas or give us feedback