News  [SoftwareSite

Latest News
Older News
RSS Feed
Complete Projects
Useful Classes
Top Downloads
Message Board
Send Comments
Software License Buttons
Forums -> Security Library Forum
POP3S Client example?  
by Nilson [nbonadeu at hotmail dot com]
posted on 2004/01/23

I need to wrote a POP3S Client and I couldn´t find an example. Someone can help me, please?

(I tried to change SMPT Client example, but it didn´t work.)

by Nilson
posted on 2004/01/24

Please, change 'wrote' by 'write' in the fisrt line of previous message.

by Pieter Philippaerts [Pieter at mentalis dot org]
posted on 2004/01/25

Take a look at [scroll down to "POP3 STARTTLS extension"].

by nbonadeu
posted on 2004/01/26

Thanks for your message. I had read it, but my problem is more specific, how can I implement POP3S using mentalis security library.

I have server, user, password, certificate (.cer file) and when I try to receive information from server after STARTLS I receive an error: UntrustedRoot. I´m afraid I changed SMTP example incluiding certificate informations in a wrong way.

What is the correct use of methods from mentalis security library to connect a server(using certificate), to retrieve message and to close connection?

by nbonadeu
posted on 2004/01/26

The error that I received is:

An error occurred [An error occurs while communicating with the remote host. Org.Mentalis.Security.Certificates.CertificateException: Could not acquire crypto context.

Is it help to find the problem?

by nbonadeu
posted on 2004/01/27

I´m sending source code, if someone can help me, I´ll apreciate:

using System;
using System.IO;
using System.Text;
using System.Net;
using System.Net.Sockets;
using Org.Mentalis.Security.Ssl;
using Org.Mentalis.Security.Certificates;

/// <summary>
/// This example program show how to send email via an Pop3 server. Apart from the normal
/// unencrypted Pop3 connections, it also supports TLS encrypted connections.
/// </summary>
class Pop3Client
static void Main(string[] args) {
Console.WriteLine("Exemplo de POP3 e POP3S\r\n");
Pop3Client s = new Pop3Client();
Console.WriteLine("\r\nPressione ENTER para continuar...");
public void Start()
Console.WriteLine("Choose type: \r\n0-POP3\r\n1-POP3S");
string type = Console.ReadLine().Trim();
if (Array.IndexOf(new string[]{"0", "1"}, type) == -1)
Console.WriteLine("Invalid type.");
string server, port, address, password;
if (type == "0")
Console.WriteLine("Server: " + server);
Console.WriteLine("Port: "+port);
Console.WriteLine("Connection type (0 = normal, 1 = TLS): "+type);
Console.WriteLine("E-mail : " + address);
Console.WriteLine("Password: " + password);

// create a new SecurityOptions instance
SecurityOptions options = new SecurityOptions(SecureProtocol.None);
// allow only secure ciphers to be used. Currently, the seure ciphers are:
// the AES algorithm [128 or 256 bit keys], the RC4 algorithm [128 bit keys]
// or TipleDES [168 bit keys]
options.AllowedAlgorithms = SslAlgorithms.SECURE_CIPHERS;
// the SecureSocket should be a client socket
options.Entity = ConnectionEnd.Client;
// we will use manual verification of the server certificate
options.VerificationType = CredentialVerification.Manual;
// when the server certificate is receives, the SecureSocket should call
// the OnVerify method
options.Verifier = new CertVerifyEventHandler(OnVerify);
// use the default flags
options.Flags = SecurityFlags.Default;
// the common name is the domain name or IP address of the server
options.CommonName = server;
// if the user chose a direct TLS connection, set the protocol to TLS1
if (type == "1")
options.Protocol = SecureProtocol.Tls1 | SecureProtocol.Ssl3;
// create the new secure socket
Connection = new SecureSocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp, options);
// connect to the Pop3 server
Connection.Connect(new IPEndPoint(Dns.Resolve(server).AddressList[0], int.Parse(port)));
// wait for the server hello message
string str = Receive();
if (!IsReplyOK(str))
Console.WriteLine("Server disallowed connection.");
Send("USER " + address + "\r\n");
if (!IsReplyOK(Receive())) {
Console.WriteLine("USER failed.");
Send("PASS "+password+"\r\n");
if (!IsReplyOK(Receive()))
Console.WriteLine("PASS failed.");
Send("RETR 2\r\n");
string buffer=Receive();
if (!IsReplyOK(buffer)) {
Console.WriteLine("RETR 2 failed.");
if (!IsReplyOK(Receive())) {
Console.WriteLine("QUIT failed.");
} catch (Exception e) {
Console.WriteLine("An error occurred [" + e.Message + "]");
} finally {
if (Connection != null) {
/// <summary>
/// This method is called when the SecureSocket received the remote
/// certificate and when the certificate validation type is set to Manual.
/// </summary>
/// <param name="socket">The <see cref="SecureSocket"/> that received the certificate to verify.</param>
/// <param name="remote">The <see cref="Certificate"/> of the remote party to verify.</param>
/// <param name="chain">The <see cref="CertificateChain"/> associated with the remote certificate.</param>
/// <param name="e">A <see cref="VerifyEventArgs"/> instance used to (in)validate the certificate.</param>
/// <remarks>If an error is thrown by the code in the delegate, the SecureSocket will close the connection.</remarks>
protected void OnVerify(SecureSocket socket, Certificate remote, CertificateChain chain, VerifyEventArgs e) {
// get all the certificates from the certificate chain ..
Certificate[] certs = chain.GetCertificates();
// .. and print them out in the console
for(int i = 0; i < certs.Length; i++)
// print out the result of the chain verification
Console.WriteLine(chain.VerifyChain(socket.CommonName, AuthType.Server));
// send data to the Pop3 server
protected void Send(string data) {
byte[] toSend = Encoding.ASCII.GetBytes(data);
int sent = Connection.Send(toSend);
while(sent != toSend.Length) {
sent += Connection.Send(toSend, sent, toSend.Length - sent, SocketFlags.None);
// receive a reply from the Pop3 server
protected string Receive() {
string reply = "";
byte[] buffer = new byte[1024];
int ret = Connection.Receive(buffer);
while(ret > 0) {
reply += Encoding.ASCII.GetString(buffer, 0, ret);
if (IsComplete(reply))
ret = Connection.Receive(buffer);
return reply;
// check whether the reply of the server is complete
protected bool IsComplete(string reply) {
return true;
// check whether the reply of the server is positive
protected bool IsReplyOK(string reply) {
if (reply.Length < 3)
return false;
else if (reply.Substring(0,3) != "+OK")
return false;
return true;

// the connection with the Pop3 server
protected SecureSocket Connection {
get {
return m_Connection;
set {
m_Connection = value;
private SecureSocket m_Connection;


Copyright © 2002-2007, The Team. All rights reserved.
This site is located at
Send comments to the webmaster.