Java Mail:如何在没有全局系统属性的情况下使用 SOCKS 进行 IMAP 处理?

发布于 2024-12-14 19:22:29 字数 380 浏览 6 评论 0原文

我有这个问题中描述的问题 JavaMail:如何对不同线程使用不同的 SOCKS5 ?

..但是这个问题没有真正的答案:-(

另外我想从 imap(s) 文件夹中检索邮件,但我不知道如何告诉javaMail使用socks代理而不通过全局系统属性进行设置(sockProxyHost等...) 如果我这样做,并行数据库请求也想使用socks代理,但它们不应该(数据库无法通过socks代理访问)

提前非常感谢您的任何提示。 汉斯

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

奈何桥上唱咆哮 2024-12-21 19:22:29

我和同事一起解决了这个问题,

使用socks代理你必须执行以下操作..

在mail.jar中你可以找到SocketFetcher类。在此类中,将检查是否通过系统属性设置了会话工厂对象或类名称。我实现了从 SSLSocketFactory 复制我自己的 SocketFactory,并且必须在 javaMail 中操作 SocketFetcher,并且我替换了类文件以从我自己的 SocketFactory 调用 createSocket(host, port) 方法。在那里我使用了代理,

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);

另外我还必须操作 SocketFetcher.createSocket()
...

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

...您必须检查套接字是否已连接,否则将引发异常,并且将使用不属于您的默认 socketFactory 祝你

好运:-)

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 :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文