Monthly Archives: January 2011

POP3 Server with Explicit security mode

Explicit connection and Implicit connection are two secure methods to connect to a secure SMTP/POP3/POP3 server. This topic will go in detail the Explicit connection.

Explicit connection

When the client connects to the server using SSL, an SSL negotiation is initialized, the connection is secured and all following comminuation is being protected.

The code snippet below shows how to connect to an POP3 server securely using Explicit security mode.

C#:

// Create a new instance of the Pop3Client class.
Pop3Client client = new Pop3Client();
// Connect to the server.
client.Connect("myserver", 143, SecurityMode.Explicit);
// Login to the server.
client.Authenticate("user", "password");
StringBuilder sb = new StringBuilder();
Pop3MessageCollection list = client.ListMessages(EnvelopeParts.Size | EnvelopeParts.UniqueId);
for (int i = 0; i < list.Count; i++)
{
   sb.AppendFormat("{0} - {1}\r\n", i + 1, list[i].UniqueId);
}
Console.WriteLine(sb.ToString());
// Close the connection.
client.Disconnect();

VB.NET:

' Create a new instance of the Pop3Client class.
Dim client As New Pop3Client()
' Connect to the server.
client.Connect("myserver", 143, SecurityMode.Explicit)
' Login to the server.
client.Authenticate("user", "password")
Dim sb As New StringBuilder()
Dim list As Pop3MessageCollection = client.ListMessages(EnvelopeParts.Size Or EnvelopeParts.UniqueId)
For i As Integer = 0 To list.Count - 1
    sb.AppendFormat("{0} - {1}" & Constants.vbCrLf, i + 1, list(i).UniqueId)
Next i
Console.WriteLine(sb.ToString())
' Close the connection.
client.Disconnect()

Sending a custom command to an IMAP server

Explicitly sending commands to an IMAP server is usually not necessary. Almost all commands that could conceivably be sent to an IMAP server are encapsulated by high-level methods. However, to send a command to an IMAP server, simply call the SendCommand method, and to get the response from the IMAP server, call the ReadResponse method.

The example below sends the “STAT” command to an IMAP server and print out the response:

C#:

// IMAP server information.
const string serverName = "myserver";
const string user = "name@domain.com";
const string password = "mytestpassword";
const int port = 993;
const SecurityMode securityMode = SecurityMode.Implicit;
// Create a new instance of the ImapClient class.
ImapClient client = new ImapClient();
// Connect to the server.
client.Connect(serverName, port, securityMode);
// Login to the server.
client.Authenticate(user, password);
// Select 'INBOX' mailbox.
client.Select("INBOX");
// Send a command.
client.SendCommand("STAT");
// Read response from the server.
ImapResponse response = client.ReadResponse();
// Print out the response.
Console.WriteLine(response.RawResponse);
// Close the connection.
client.Disconnect();

VB.NET:

' IMAP server information.
Const serverName As String = "myserver"
Const user As String = "name@domain.com"
Const password As String = "mytestpassword"
Const port As Integer = 993
Const securityMode As SecurityMode = securityMode.Implicit
' Create a new instance of the ImapClient class.
Dim client As New ImapClient()
' Connect to the server.
client.Connect(serverName, port, securityMode)
' Login to the server.
client.Authenticate(user, password)
' Select 'INBOX' mailbox.
client.Select("INBOX")
' Send a command.
client.SendCommand("STAT")
' Read response from the server.
Dim response As ImapResponse = client.ReadResponse()
' Print out the response.
Console.WriteLine(response.RawResponse)
' Close the connection.
client.Disconnect()