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:
- First it creates proxy with specified type (ProxyType), proxy address and port using proxy factory
- Then it connects to IMAP, POP3 or SMTP server via proxy and creates a socket
- 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.
April 1st, 2011 at 01:30
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!
April 1st, 2011 at 09:23
@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(); }April 1st, 2011 at 19:12
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…
April 2nd, 2011 at 09:31
@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.
April 4th, 2011 at 19:05
I have tried with a different proxy and it worked! I think I just selected invalid proxies previous times.
April 5th, 2011 at 16:14
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?
April 5th, 2011 at 17:06
@VMedvedeva
Is the network enabled?
Have you disabled your anti-virus software?
October 4th, 2011 at 00:48
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.
October 4th, 2011 at 17:34
@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
October 6th, 2011 at 14:38
I’ve downloaded your mail package in zip and installer, but cant find Proxy.dll library in this package.
October 6th, 2011 at 16:29
@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