Imap, Pop3 or Smtp via HTTP or SOCKS proxy

Mail.dll supports following proxy protocols:

  • HTTP
  • SOCKS5
  • SOCKS4a
  • SOCKS4

As a prerequisite you need to add reference to Mail.dll and Proxy.dll libraries in your project. You can find both assemblies in the Mail.dll download package. You can also find both assemblies on the .NET tab in Visual Studio’s “Add reference” dialog after installation.

Following sample uses HTTP proxy to access IMAP server:

  1. First it creates proxy with specified type (ProxyType), proxy address and port using proxy factory
  2. Then it connects to IMAP, POP3 or SMTP server via proxy and creates a socket
  3. Finally it attaches socket to IMAP, POP3 or SMTP client
// C# version

ProxyFactory factory = new ProxyFactory();
IProxyClient proxy = factory.CreateProxy(ProxyType.Http, "221.3.154.9", 80);

Socket socket = proxy.Connect("imap.example.org", Imap.DefaultPort);

using (Imap imap = new Imap())
{
    imap.Attach(socket);

    // regular imap code

    imap.Close();
}
' VB.NET version

Dim factory As New ProxyFactory()
Dim proxy As IProxyClient = factory.CreateProxy(ProxyType.Http, "221.3.154.9", 80)

Dim socket As Socket = proxy.Connect("imap.example.org", Imap.DefaultPort)

Using imap As New Imap()
   imap.Attach(socket)

   ' regular imap code

   imap.Close()
End Using

Following sample uses HTTP proxy to access IMAP server over SSL connection. Note that we need to pass IMAP server name again to AttachSSL method for SSL authentication. Please also note that Imap.DefaultSSLPort constant is used.

// C# version

ProxyFactory factory = new ProxyFactory();
IProxyClient proxy = factory.CreateProxy(
   ProxyType.Http, "221.3.154.9", 80);

Socket socket = proxy.Connect("imap.gmail.com", Imap.DefaultSSLPort);

using (Imap imap = new Imap())
{
    imap.AttachSSL(socket, "imap.gmail.com");

    // regular imap code

    imap.Close();
}
' VB.NET version

Dim factory As New ProxyFactory()
Dim proxy As IProxyClient = factory.CreateProxy( _
   ProxyType.Http, "221.3.154.9", 80)

Dim socket As Socket = proxy.Connect("imap.gmail.com", Imap.DefaultSSLPort)

Using imap As New Imap()
   imap.AttachSSL(socket, "imap.gmail.com")

   ' regular imap code

   imap.Close()
End Using

The same code works for Smtp and Pop3 clients: you only need to use different port (Smtp.DefaultPort, Smtp.DefaultSSLPort, Pop3.DefaultPort, Pop3.DefaultSSLPort or any other port your server uses) when creating the proxy, and create appropriate client in ‘using’ line.

Tags: , , , , ,

11 Responses to “Imap, Pop3 or Smtp via HTTP or SOCKS proxy”

  1. phoenicyan Says:

    Thank you for the great product and the blog! However, when I tried to connect to imap.google.com via a proxy I got the exception:
    “Proxy responded with a 302 code – Found.”

    How this should be handled? Here is my code:

    ProxyFactory factory = new ProxyFactory();

    string host = “64.27.0.30″;
    IProxyClient proxy = factory.CreateProxy(ProxyType.Http, host, 80);

    Console.WriteLine(“Connecting…”);

    try
    {
    Socket socket = proxy.Connect(“imap.gmail.com”, Imap.DefaultPort);

    Console.WriteLine(“Connected!”);

    Imap imapClient = new Imap();
    imapClient.AttachSSL(socket, “imap.gmail.com”);

    Console.WriteLine(“Attached!”);

    imapClient.Close();
    }
    catch (Exception e)
    {
    Console.WriteLine(“{0}”, e.Message);
    }

    Thank you!

  2. Limilabs support Says:

    @phoenicyan
    You need to connect to imap.gmail.com over SSL:

    Your code should look as follows (note Imap.DefaultSSLPort constant):

    string host = "62.27.0.30";
    IProxyClient proxy = factory.CreateProxy(ProxyType.Http, host, 80);
    
    Socket socket = proxy.Connect("imap.gmail.com", Imap.DefaultSSLPort);
    
    using(Imap imap = new Imap())
    {
        imap.AttachSSL(socket, "imap.gmail.com");
        imap.Close();
    }
    
  3. phoenicyan Says:

    Thank you for the quick response. I have tried using DefaultSSLPort as well, but got the same exception – “302 code”. Does this code work on your computer? Is it possible that it is not working on our systems due to limitations of our corporate LAN?

    I have configured the proxy in FireFox and it looked like working…

  4. Limilabs support Says:

    @phoenicyan
    I can not connect to the proxy you are using.
    I’ve send you an email, so we can work on this issue together.

    The error you are getting is returned by the proxy, and it’s a redirect. It does not seem like a problem with Mail.dll.

    The code is definitely working – I’ve tried with many different proxy servers.

  5. phoenicyan Says:

    I have tried with a different proxy and it worked! I think I just selected invalid proxies previous times.

  6. VMedvedeva Says:

    Hello! I try to use your dll to connect “pop.gmail.com” and get an exception
    “An attempt of operation on socket with network disabled
    95.84.187.10:8080

    at System.Net.Sockets.Socket.Connect(IPAddress[] addresses, Int32 port)
    at System.Net.Sockets.Socket.Connect(String host, Int32 port)
    at Limilabs.Proxy.HttpProxy.HttpProxyClient.Connect(String destinationHost, Int32 destinationPort)
    at Pop.Program.Main(String[] args) в C:Pop3MailClientPop3MailClientPop3MailClientProgram.cs:строка 27″.

    Could you tell me what I do wrong?

  7. Limilabs support Says:

    @VMedvedeva

    Is the network enabled?
    Have you disabled your anti-virus software?

  8. jgw Says:

    I have a problem; I’m trying to use a socks5 proxy, which I know is working, but it parses the error that I require authentication (username, password) for the socks5. I know it doesn’t require authentication. Any help would be appreciated.

  9. Limilabs support Says:

    @jgw

    If the error says you need to authenticate why do you claim it doesn’t require authentication?
    Please contact us directly as I don’t think we can help you via blog

  10. Matvey Says:

    I’ve downloaded your mail package in zip and installer, but cant find Proxy.dll library in this package.

  11. Limilabs support Says:

    @Matvey

    You can find it in:
    Mail.zip/Redistributables/Proxy/Proxy.dll

    or when you installed Mail.dll in:
    C:/Program Files/Limilabs/Mail/Redistributables/Proxy/Proxy.dll

Leave a Reply