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:
- 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.
March 31st, 2015 at 23: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);
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!
April 1st, 2015 at 07:23
@phoenicyan
You need to connect to imap.gmail.com over SSL:
Your code should look as follows (note Imap.DefaultSSLPort constant):
April 1st, 2015 at 17: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, 2015 at 07: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, 2015 at 17:05
I have tried with a different proxy and it worked! I think I just selected invalid proxies previous times.
April 5th, 2015 at 14: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, 2015 at 15:06
@VMedvedeva
Is the network enabled?
Have you disabled your anti-virus software?
October 3rd, 2015 at 22: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, 2015 at 15: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, 2015 at 12:38
I’ve downloaded your mail package in zip and installer, but cant find Proxy.dll library in this package.
October 6th, 2015 at 14: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
September 15th, 2016 at 03:42
Hi,
how does the SPF rule be affected by using a proxy to send mails in limilabs?
Regards,
Jennath
September 15th, 2016 at 09:36
@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.
October 16th, 2016 at 05:02
I am not able to find proxy address. What is this? where I can find it?
October 16th, 2016 at 05:30
@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
October 16th, 2016 at 06:04
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
October 16th, 2016 at 10:27
@Ali
You need an HTTP proxy that allows SMTP traffic. I can’t recommend any.
October 25th, 2016 at 16:57
Is there any timeout settings, seems like proxy.Connect() freezes when proxy is dead.
October 25th, 2016 at 17:07
@Anton,
All proxy clients use 20 seconds send and receive timeouts.
October 26th, 2016 at 16:12
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.
October 28th, 2016 at 14:09
@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.
March 14th, 2018 at 00:55
Hi, is it possible to use authentication with the http proxy server? Because currently I just get (407) Authentication required error
March 14th, 2018 at 06:18
@Karthik,
It is possible. Please use ProxyFactory.CreateProxy overload which takes authentication parameters: