Kristian
Kristian

Reputation: 1378

SMTP over Socks Proxy

I use the following code for SMTP:

            MailMessage mail = new MailMessage();
            mail.From = new System.Net.Mail.MailAddress(Global.username);
            mail.Subject = Global.subject;

            SmtpClient smtp = new SmtpClient();
            smtp.Port = 587;
            smtp.EnableSsl = true;
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtp.UseDefaultCredentials = false;
            smtp.Credentials = new NetworkCredential(Global.username, Global.password); 
            smtp.Host = "smtp.gmail.com";

            mail.To.Add(new MailAddress(Global.to));

            mail.IsBodyHtml = true;
            mail.Subject = Global.subject;
            mail.Body = Global.message;
            smtp.Send(mail);

That works perfectly, but the problem is I need HTTP and Socks proxy support. I'm looking for ether free libraries that I can use for commercial purposes or what approach should I do to get proxies running. I also need this for POP3 and IMAP, I'm thinking it will be easier to write my own wrappers than finding something already out there, I'm just not sure where to start. I have got my own HTTP wrapper done with HTTP and Socks support, but I'm not sure how SMTP, IMAP and POP3 works through proxy.. Whats the best way for me to get this done? Thanks!

Upvotes: 0

Views: 3941

Answers (3)

Sumit Joshi
Sumit Joshi

Reputation: 1115

I would suggest Aspose.Email library.

Sample code:

SmtpClient client = new SmtpClient("smtp.domain.com", "username", "password");
client.SecurityOptions = SecurityOptions.SSLImplicit;
string proxyAddress = "192.168.203.142"; // proxy address
int proxyPort = 1080; // proxy port
SocksProxy proxy = new SocksProxy(proxyAddress, proxyPort, SocksVersion.SocksV5);
client.Proxy = proxy;
client.Send(new MailMessage("[email protected]", "[email protected]", "Sending Email via proxy", "Implement socks proxy protocol for versions 4, 4a, 5 (only Username/Password authentication)"));

Upvotes: 0

Annie Lagang
Annie Lagang

Reputation: 3235

How about using CNTLM? I'd used it before for Python and I haven't tried it with other programming languages.

Upvotes: 3

Roman Ryltsov
Roman Ryltsov

Reputation: 69672

Socks support needs to be embedded into SMTP class itself (or at least, its part which manages socket connections), which is not the case here. You are left with two options:

  1. to use a replacement for MailMessage, which has support for SOCKS built in
  2. or, you need to use a SOCKS proxy/tunnel application or class, which would open a listening socket on local system and would accept connections on it. Then it would act as SOCKS client and would pass data through both ways. You will set your MailMessage instance to work with this local port and the data will be properly wrapped for you to go though SOCKS.

Upvotes: 1

Related Questions