This discussion is archived
3 Replies Latest reply: Jul 20, 2012 4:48 AM by 950718 RSS

Socks Proxy Authentication

950718 Newbie
Currently Being Moderated
I have working socks proxy with Java Mail 1.4.5 without user name/password but wish to support socks proxies that require user names and passwords.

I have tried using an authenticator on the Session.getInstance(Properties, Authenticator) object but I get an error saying incorrect user name/password on my socks proxy.

My sample code is below:

Properties properties = getProperties();
final String socksProxyUserName = "test";
final String socksProxyPassword = "test";

Authenticator authenticator = new Authenticator() {

     @Override
     protected PasswordAuthentication getPasswordAuthentication() {
          PasswordAuthentication passwordAuthentication = new PasswordAuthentication(socksProxyUserName, socksProxyPassword);
          return passwordAuthentication;
     }
};

Session session = Session.getInstance(properties, authenticator);

My socks proxy is FreeProxy.

I am using this method for socks proxy support in java mail.
If your proxy server supports the SOCKS V4 or V5 protocol (http://www.socks.nec.com/aboutsocks.html, RFC1928) and allows anonymous connections, and you're using JDK 1.5 or newer and JavaMail 1.4.5 or newer, you can configure a SOCKS proxy on a per-session, per-protocol basis by setting the "mail.smtp.socks.host" property as described in the javadocs for the com.sun.mail.smtp package. Similar properties exist for the "imap" and "pop3" protocols.


Message in Proxy logfile is:
Fri 20 Jul 2012 09:37:59 : ACCESS : Instance:'socky' Protocol:'SOCKS-5-Proxy' Access:'Forbidden' Client IP:'10.45.16.21' User:'test/FPDOMAIN' Resource Type:'User Authentication' Resource:'Authentication: User/password invalid'

I have confirmed user name and password several times and tried extra ones just in case but no luck.

Does anybody have any ideas. THe fact that it tries to authenticate the login on my proxy server suggests I'm trying the correct method to connect to me but can someone confirm this for me?

This proxy has this user added as I set it up myself.

Edited by: 947715 on 20-Jul-2012 01:15

Edited by: 947715 on 20-Jul-2012 01:16

Edited by: 947715 on 20-Jul-2012 01:40

Edited by: 947715 on 20-Jul-2012 03:10
  • 1. Re: Socks Proxy Authentication
    EJP Guru
    Currently Being Moderated
    I have tried using an authenticator on the Session.getInstance(Properties, Authenticator) object but I get an error saying incorrect user name/password on my socks proxy.
    That's because the Authenticator is for establishing the Session with the mail server, not for talking to the proxy. it's the mail-server's username/password, not the proxy's.

    I'm not aware that Java supports authentication to any kind of proxy. I believe the Apache HttpClient does, for HTTP proxies, and there may be something in there to talk to SOCKS proxies as well.
  • 2. Re: Socks Proxy Authentication
    950718 Newbie
    Currently Being Moderated
    Ok, I'll try via another method. I can't use Apache HttpClient so I'll see if I can find something else.

    If anyone has any suggestions, do let me know.
  • 3. Re: Socks Proxy Authentication
    950718 Newbie
    Currently Being Moderated
    I got it working now using the below:

    final String socksProxyUserName = configurationService.getString(IParameterConstants.SOCKS_PROXY_USERNAME);
    final String socksProxyPassword = configurationService.getString(IParameterConstants.SOCKS_PROXY_PASSWORD);

    if (!socksProxyUserName.equals(EMPTY_STRING)) {
         java.net.Authenticator authenticator = new java.net.Authenticator() {
    @Override
         protected java.net.PasswordAuthentication getPasswordAuthentication() {
              return new java.net.PasswordAuthentication(socksProxyUserName, socksProxyPassword.toCharArray());
         }

         };

         System.setProperty("java.net.socks.username", socksProxyUserName); //$NON-NLS-1$
         System.setProperty("java.net.socks.password", socksProxyPassword); //$NON-NLS-1$
         java.net.Authenticator.setDefault(authenticator);

    }

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points