Glossary Item Box

PowerTCP Mail for .NET

Sending Commands To A POP Server

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. For instructions on how to do this see Placing Components on a Form.
  2. Import the namespace of the component you would like to use using "Using" (C#) or "Imports" (VB.NET).

    [Visual Basic, C#] Place the following code at the top of your file.

    [C#]
    using Dart.PowerTCP.Mail;
    
    [Visual Basic]
    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.

    [Visual Basic, C#] Place the following code in the Click event of the button added to the form in step 3.

    [C#]
    // Do not automatically get messages after logging in.
    pop1.AutoGet = MessageSection.None;
    
    // Do not close the connection.
    pop1.AutoLogout = false;
    
    [Visual Basic]
    ' Do not automatically get messages after logging in.
    Pop1.AutoGet = None
    
    ' Do not close the connection.
    Pop1.AutoLogout = False
    
  5. Login to the server.

    [Visual Basic, C#] Place this code directly after the code from step 5.

    [C#]
    // Login
    pop1.Login("mail.test.com", "test", "pass");
    
    [Visual Basic]
    ' Login
    Pop1.Login("mail.test.com", "test", "pass")
    
  6. Send the "STAT" command.

    [Visual Basic, C#]

    [C#]
    pop1.Connection.Send("STAT\r\n");
    
    [Visual Basic]
    Pop1.Connection.Send("STAT" + vbCrLf)
    
  7. Receive the response.

    [Visual Basic, C#]

    [C#]
    // Receive the response.
    Segment seg = pop1.Connection.Receive();
    
    [Visual Basic]
    ' Receive the response.
    Dim seg As Segment = Pop1.Connection.Receive()
    
  8. Parse and display the response.

    [Visual Basic, C#]

    [C#]
    // 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]
    ' 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()
    

 

In This Section

Getting All Messages
Provides a description of the simplest and easiest way to login and get all available mail message.
Getting All Message Headers
Provides a description of the simplest and easiest way to login and get all available message headers.
Getting A Single Message
Demonstrates how to login in and only get one message.
Getting Messages Asynchronously
Demonstrates how asynchronous methods can be used with the Pop component.
Performing a TOP
Shows how to get a specified number of lines of a message.
Decoding Attachments As Files
Demonstrates how to get messages and decode their attachments as files.
Decoding Attachments As Streams
Demonstrates how to get messages and decode their attachments as Streams.
Displaying Progress While Getting Mail
Provides a description of how to display progress when getting mail.
Stream Use When Getting Mail
Provides a discussion on how Streams are used by the Pop component to make getting mail more flexible.
Sending Commands To A POP Server
Shows how to send a command to a POP server and receive the response.
Making a Trace Log When Getting Mail
Provides a description of how to create a log file containing all data and commands sent or received over the TCP connection.

 

 


Send comments on this topic.

Documentation version 3.1.

© 2009 Dart Communications.  All rights reserved.