andyedinborough / aenetmail

C# POP/IMAP Mail Client
369 stars 153 forks source link

Working with a proxy #21

Open piher opened 12 years ago

piher commented 12 years ago

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); }

JackEker commented 12 years ago

I don't quite understand this however I would very much like to! I was referred to this post from my question on Stack Overflow (http://stackoverflow.com/questions/11080214/how-to-receive-email-using-imap-from-gmail-whilst-on-proxy-with-firewall) but as I say I can't quite make heads nor tails of the solution! I would greatly appreciate any help you could give!

piher commented 11 years ago

Hi JackEker, I'm really sorry for the (super) late reply, the project in wich I was using the library is now over. Actually when I wrote this post I was hoping that someone else would be interested in the matter because my attempts to implement this functionnality weren't succesful :/ I think the general idea is there but more work would need to be done with the starksoft library which is supposed to work. I hope that this wasn't a key functionnality in your project...