RenneB
RenneB

Reputation: 41

Java Mail: How to use SOCKS for IMAP processing without global sytem properties?

I have the problem described in this question JavaMail: How to use different SOCKS5 for different threads?

..but there is no really answer to this question :-(

additionally I want to retrieve mails from an imap(s) folder and I don't know how to tell javaMail to use a socks proxy without setting via global system properties. (sockProxyHost and so on...) If I do so parallel database requests also want to use the socks proxy but they shouldn't (db is not accessible via socks proxy)

Thanks a lot in advance for any hint. Hans

Upvotes: 1

Views: 1719

Answers (1)

RenneB
RenneB

Reputation: 41

I solved it together with a colleague

to use a socks proxy you have to do the following..

inside the mail.jar you can find the SocketFetcher class. within this class it is checked if a session factory object or class name is set via system properties. I implemented my own SocketFactory copying from SSLSocketFactory and I had to manipulate the SocketFetcher inside javaMail and I replaced the class file to call createSocket(host, port) method from my own SocketFactory. And there I used a proxy to

String proxyHost = System.getProperty(SYSTEM_PROP_SOCKS_PROXY_HOST);
int proxyPort = Integer.parseInt(System.getProperty(SYSTEM_PROP_SOCKS_PROXY_PORT));

SocketAddress addr = new InetSocketAddress(proxyHost, proxyPort);
Proxy proxy = new Proxy(Proxy.Type.SOCKS, addr);
socket = new Socket(proxy);

additionally I had to manipulate SocketFetcher.createSocket() ...

socket.connect(new InetSocketAddress(host, port));

...you have to check if the socket is already connected otherwise an exception will be thrown and the default socketFactory will be used which isn't yours

Lots of luck :-)

Upvotes: 3

Related Questions