Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Working with a proxy #21

Open
piher opened this issue Jan 18, 2012 · 3 comments
Open

Working with a proxy #21

piher opened this issue Jan 18, 2012 · 3 comments

Comments

@piher
Copy link
Contributor

piher commented Jan 18, 2012

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
Copy link

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
Copy link
Contributor Author

piher commented Nov 13, 2012

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...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants
@piher @JackEker and others