andyedinborough/aenetmail — Issue #21: Working with a proxy
Author: piher
Created: 2012-01-18 13:13:28+00:00
State: open

Hello,

I am exceptionally on a network that requires a proxy configuration.
I thought I would try out my email software on this network, and as it turns out the TcpClient object doesn't work on proxies and doesn't seem to allow any proxy configuration.

Have you used your library through a proxy ?

If not, i have found a quite simple open-source C# library creating TcpClient objects through proxies :
http://www.starksoft.com/prod_proxy.html

As an example, the connect method in Imap client could become something like :

public void Connect(string hostname, int port, bool ssl, string proxyHost = null, int proxyPort = -1, ProxyType proxyType = ProxyType.None)
{
try
{
Host = hostname;
Port = port;
Ssl = ssl;

if (proxyHost != null && proxyType != ProxyType.None && proxyPort > -1)
{
// create an instance of the client proxy factory
ProxyClientFactory factory = new ProxyClientFactory();

// use the proxy client factory to generically specify the type of proxy to create
// the proxy factory method CreateProxyClient returns an IProxyClient object
IProxyClient proxy = factory.CreateProxyClient(proxyType, proxyHost, proxyPort);

// create a connection through the proxy to hostname over the port
_Connection = proxy.CreateConnection(hostname, port);
}
else { _Connection = new TcpClient(hostname, port); }

_Stream = _Connection.GetStream();
if (ssl)
{
var sslSream = new System.Net.Security.SslStream(_Stream);
_Stream = sslSream;
sslSream.AuthenticateAsClient(hostname);
}

_Reader = new StreamReader(_Stream, System.Text.Encoding.Default);
string info = _Reader.ReadLine();
OnConnected(info);

IsConnected = true;
Host = hostname;
}
catch (Exception)
{
IsConnected = false;
throw;
}
}

and the imap constructor :

public ImapClient(string host, string username, string password, AuthMethods method = AuthMethods.Login, int port = 143, bool secure = false, string proxyHost = null, int proxyPort = -1, ProxyType proxyType = ProxyType.None) {
Connect(host, port, secure, proxyHost, proxyPort, proxyType);
AuthMethod = method;
Login(username, password);
}