PowerTCP Mail for .NET
Sending POP Commands
Send comments on this topic.



Glossary Item Box

It is usually not necessary to explicitly send commands to a POP server. Almost all commands that could conceivably be sent to a POP server are encapsulated by high-level methods. For example, depending on your Pop component settings, calling Login could cause the "USER", "PASS", "STAT", "LIST", and "RETR" commands to be automatically sent to the server. There are rare occasions when you must send a command to a POP server that is not encapsulated by a high-level method. This is often the case with a POP server that is operating outside of the RFC spec that requires proprietary commands.

The Pop component uses the Tcp component for the connection, which is exposed through the Connection property. To send commands, simply call the methods of this object (such as Pop.Connection.Send to send commands and Pop.Connection.Receive to receive commands)

To send a command to a POP server, use the following steps. For the purposes of this example, the "STAT" command (which causes the POP server to return the amount of messages and size of all messages) is used.

  1. Add the Pop component to a new form.
  2. Import the namespace of the component you would like to use using "Using" (C#) or "Imports" (VB.NET). Place the following code at the top of your file.
    C# Copy Code
    using Dart.PowerTCP.Mail;
    Visual Basic Copy Code
    Imports Dart.PowerTCP.Mail
  3. Add a button to the form.
  4. For this example, we would like to login to the POP server using the Pop.Login method, and remain logged in. This will enable us to send commands. Place the following code in the Click event of the button added to the form in step 3.
    C# Copy Code
    // Do not automatically get messages after logging in.
    pop1.AutoGet = MessageSection.None;
    
    // Do not close the connection.
    pop1.AutoLogout = false;
    Visual Basic Copy Code
    ' Do not automatically get messages after logging in.
    Pop1.AutoGet = None
    
    ' Do not close the connection.
    Pop1.AutoLogout = False
  5. Login to the server. Place this code directly after the code from step 5.
    C# Copy Code
    // Login
    pop1.Login("mail.test.com", "test", "pass");
    Visual Basic Copy Code
    ' Login
    Pop1.Login("mail.test.com", "test", "pass")
  6. Send the "STAT" command.
    C# Copy Code
    pop1.Connection.Send("STAT\r\n");
    Visual Basic Copy Code
    Pop1.Connection.Send("STAT" + vbCrLf)
  7. Receive the response.
    C# Copy Code
    // Receive the response.
    Segment seg = pop1.Connection.Receive();
    Visual Basic Copy Code
    ' Receive the response.
    Dim seg As Segment = Pop1.Connection.Receive()
  8. Parse and display the response.
    C# Copy Code
    // Split the response.
    char ch = ' ';
    string[] responseparts = seg.ToString().Split(ch);
    
    // Check for good response.
    if(responseparts[0] == "+OK")
    {
       Debug.WriteLine("Number of messages: " + responseparts[1]);
       Debug.WriteLine("Size of messages: " + responseparts[2]);
    }
    else
    {
       // Bad response from server.
       Debug.WriteLine(seg.ToString());
    }
    
    // Be sure to close the connection.
    pop1.Close();
    Visual Basic Copy Code
    ' Split the response.
    Dim ch As Char = " "
    Dim responseparts() = seg.ToString().Split(ch)
    
    ' Check for good response.
    If responseparts(0) = "+OK" Then
       Debug.WriteLine("Number of messages: " + responseparts(1))
       Debug.WriteLine("Size of messages: " + responseparts(2))
    Else
       ' Bad response from server.
       Debug.WriteLine(seg.ToString())
    End If
    
    ' Be sure to close the connection.
    Pop1.Close()
Documentation Version 3.2
© 2010 Dart Communications. All Rights Reserved.