Library tutorials & articles

How to POP3 in C#

Connecting and Disconnecting

The first method of our Pop3 class is the Connect method. This method takes a server name, username and password parameter to connect to a remote (sometimes local) POP3 server.

public void Connect(string server, string username, string password)
{
    string message;
    string response;

    Connect(server, 110);
    response = Response();
    if (response.Substring(0, 3) != "+OK")
    {
        throw new Pop3Exception(response);
    }

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

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

We begin by calling the TcpClient.Connect method passing the server name and the 110 port. The 110 port number is the well known port number for POP3 operations. What that means is that POP3 servers by default should listen for connections on port 110. When the POP3 server connects to a client, it should immediately respond with the +OK acknowledgement message. Next we send two messages, USER and PASS, back to the server. The POP3 server should acknowledge a successful login by acknowledging both messages. If the POP3 server returns anything but +OK, then the message will contain the reason for the failure. In the advent of a failure, I attach that failure message to our exception class and throw it back to the client. It should be noted that some POP3 servers don't require authentication and may reject the calls to USER and PASS. I haven't encountered such a POP3 server, but the protocol allows it. In those cases, you'll have to slightly modify the class to make things work.

Any use of our Pop3 class should begin with a call to Connect and end with a class to Disconnect.

public void Disconnect()
{
    string message;
    string response;
    message = "QUIT\r\n";
    Write(message);
    response = Response();
    if (response.Substring(0, 3) != "+OK")
    {
        throw new Pop3Exception(response);
    }
}

The Disconnect method sends a QUIT message to the POP3 server.

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