When you want log commands sent to the server and the corresponding responses, the following events are very useful for diagnostic purposes:
Event name |
Description |
CommandResponse |
Occurs when a command is sent to the server or a response is received from the server. |
StateChanged |
Occurs when the session state changes, such as from Disconnected to Connecting or from Downloading to Idle. |
Progress |
Occurs when a block of data is sent or received. |
The steps below show you how to handle the CommandResponse event to log commands sent to the server and responses:
Using CommandResponse event to make a trace log
- Add using directives to your code to create aliases for existing namespaces and avoid having to type the fully qualified type names. The code looks similar to the following:
using System;
using System.Text;
using ComponentPro.Net;
using ComponentPro.Net.Mail;
Imports System.Text
Imports ComponentPro.Net
Imports ComponentPro.Net.Mail
- Create a new instance of the Pop3 class.
- Register handler to the CommandResponse event.
- Now you can connect to the POP3 server with Connect methods. The code looks similar to the following:
// Create a new instance.
Pop3 client = new Pop3();
// Register event handlers.
client.CommandResponse += client_CommandResponse;
// Connect to the POP3 server.
client.Connect("server");
' Create a new instance.
Dim client As New Pop3()
' Register event handlers.
AddHandler client.CommandResponse, AddressOf client_CommandResponse
' Connect to the POP3 server.
client.Connect("server")
- Use your user name and password to login with Authenticate methods. The code looks similar to the following:
// Authenticate.
client.Authenticate("test", "test");
' Authenticate.
client.Authenticate("test", "test")
- Do your work like listing messages, downloading mail messages, etc. The code looks similar to the following:
StringBuilder sb = new StringBuilder();
Pop3MessageCollection list = client.ListMessages(Pop3EnvelopeParts.Size | Pop3EnvelopeParts.UniqueId);
for (int i = 0; i < list.Count; i++)
{
sb.AppendFormat("{0} - {1}\r\n", i + 1, list[i].UniqueId);
}
Console.WriteLine(sb.ToString());
Dim sb As New StringBuilder()
Dim list As Pop3MessageCollection = client.ListMessages(Pop3EnvelopeParts.Size Or Pop3EnvelopeParts.UniqueId)
For i As Integer = 0 To list.Count - 1
sb.AppendFormat("{0} - {1}" & vbCrLf, i + 1, list(i).UniqueId)
Next i
Console.WriteLine(sb.ToString())
- After completing your work, call the Disconnect method to close the POP3 session.
Final example code
using System;
using System.Text;
using ComponentPro.Net;
using ComponentPro.Net.Mail;
...
public void HandleCommandSentResponseReadEvents()
{
// Create a new instance.
Pop3 client = new Pop3();
// Register event handlers.
client.CommandResponse += client_CommandResponse;
// Connect to the POP3 server.
client.Connect("server");
// Authenticate.
client.Authenticate("test", "test");
// ...
// Do something here
// ...
StringBuilder sb = new StringBuilder();
Pop3MessageCollection list = client.ListMessages(Pop3EnvelopeParts.Size | Pop3EnvelopeParts.UniqueId);
for (int i = 0; i < list.Count; i++)
{
sb.AppendFormat("{0} - {1}\r\n", i + 1, list[i].UniqueId);
}
Console.WriteLine(sb.ToString());
// ...
// Disconnect.
client.Disconnect();
}
void client_CommandResponse(object sender, CommandResponseEventArgs e)
{
if (e.Command != null)
Console.WriteLine("CMD> " + e.Command);
else
Console.WriteLine("RESPONSE> " + e.Response);
}
Imports System.Text
Imports ComponentPro.Net
Imports ComponentPro.Net.Mail
...
Public Sub HandleCommandSentResponseReadEvents()
' Create a new instance.
Dim client As New Pop3()
' Register event handlers.
AddHandler client.CommandResponse, AddressOf client_CommandResponse
' Connect to the POP3 server.
client.Connect("server")
' Authenticate.
client.Authenticate("test", "test")
' ...
' Do something here
' ...
Dim sb As New StringBuilder()
Dim list As Pop3MessageCollection = client.ListMessages(Pop3EnvelopeParts.Size Or Pop3EnvelopeParts.UniqueId)
For i As Integer = 0 To list.Count - 1
sb.AppendFormat("{0} - {1}" & vbCrLf, i + 1, list(i).UniqueId)
Next i
Console.WriteLine(sb.ToString())
' ...
' Disconnect.
client.Disconnect()
End Sub
Private Sub client_CommandResponse(ByVal sender As Object, ByVal e As CommandResponseEventArgs)
If e.Command IsNot Nothing Then
Console.WriteLine("CMD> " & e.Command)
Else
Console.WriteLine("RESPONSE> " & e.Response)
End If
End Sub