Imap, Pop3 or Smtp via HTTP or SOCKS proxy

Mail.dll (IMAP, POP3 and SMTP component for .NET) 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 .NET email component 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:        

Questions?

Consider using our Q&A forum for asking questions.

23 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);
    try
    {
    Socket socket = proxy.Connect("imap.gmail.com", Imap.DefaultPort);
    Imap imapClient = new Imap();
    imapClient.AttachSSL(socket, "imap.gmail.com");
    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

  12. Jennath Says:

    Hi,

    how does the SPF rule be affected by using a proxy to send mails in limilabs?

    Regards,
    Jennath

  13. Limilabs support Says:

    @Jennath

    Sending email through proxy does not affect SPF in any way.
    You are not sending email from the proxy server, proxy server is only used to connect to the SMTP server.
    SMTP server is the one that sends an email.

  14. Ali Says:

    I am not able to find proxy address. What is this? where I can find it?

  15. Limilabs support Says:

    @Ali

    Please ask your administrator for that. If you don’t know what it is, most likely you simply don’t need it.

    You can use Mail.dll without using proxy servers:
    http://www.limilabs.com/blog/receive-unseen-emails-using-imap

  16. Ali Says:

    Basically All ports all blocked and we already use local proxy to access internet (port 80 only). and that proxy not allow to communicate to smtp or other ports

    How can we connect using only http protocol with blocked proxy server (only port 80 allowed) to connect to smtp

  17. Limilabs support Says:

    @Ali

    You need an HTTP proxy that allows SMTP traffic. I can’t recommend any.

  18. Anton Says:

    Is there any timeout settings, seems like proxy.Connect() freezes when proxy is dead.

  19. Limilabs support Says:

    @Anton,

    All proxy clients use 20 seconds send and receive timeouts.

  20. Anton Says:

    ok I loop proxy.Connect() through list of public http proxies and some specific proxies leads to OutOfmemory exception. is it a bug or some restriction should be applied to proxies ?
    P.S. can provide list of alive proxies that gives me OutOfmemory exception, if it would help.

  21. Limilabs support Says:

    @Anton,

    Most likely OutOfMemory exception is thrown by your code, not Mail.dll/Proxy.dll.
    Are you sure you are using Dispose method or using clause to release resources properly?

    If you are sure that this is a bug in our component,
    please provide us the sample code that reproduces this behavior along with the proxy address.

  22. Karthik Says:

    Hi, is it possible to use authentication with the http proxy server? Because currently I just get (407) Authentication required error

  23. Limilabs support Says:

    @Karthik,

    It is possible. Please use ProxyFactory.CreateProxy overload which takes authentication parameters:

    ProxyFactory.CreateProxy(ProxyType type, string proxyHost, int proxyPort, string proxyUser, string proxyPassword)