1

スレッドごとにデータベースからいくつかの電子メール アカウントに接続するマルチスレッド アプリケーションを作成しました。JavaMail には接続に SOCKS5 を使用するオプションがないことを知っているので、System.setProperty メソッドを介して使用することにしました。しかし、このメソッドはアプリケーション全体に SOCKS5 を設定するため、スレッドごとに 1 つの SOCKS5 を使用する必要があります。つまり:

  • 最初のスレッド: SOCKS 192.168.0.1:12345 を使用して bob@localhost に接続します
  • 2 番目のスレッド: alice@localhost の接続に SOCKS 192.168.0.20:12312 を使用します。
  • 3 番目のスレッド: SOCKS 192.168.12.:8080 を使用して andrew@localdomain に接続します

等々。これを行う方法を教えてもらえますか?

4

1 に答える 1

2

必要なプロキシを使用して独自のソケットを作成する必要があります。

SocketAddress addr = new InetSocketAddress("socks.mydomain.com", 1080);
Proxy proxy = new Proxy(Proxy.Type.SOCKS, addr);
Socket socket = new Socket(proxy);
InetSocketAddress dest = new InetSocketAddress("smtp.foo.com", 25);
socket.connect(dest);

次に、それを接続に使用します。

SMTPTransport transport = (SMTPTransport) session.getTransport("smtp");
transport.connect(socket);

編集:メールを送信するために SMTP サーバーでの認証が必要な場合は注意が必要です。その場合は、サブクラスを作成してメソッドjavax.mail.Authenticatorに渡す必要があります。Session.getInstance()

MyAuthenticator authenticator = new MyAuthenticator();

Properties properties = new Properties();
properties.setProperty("mail.smtp.submitter",
                        authenticator.getPasswordAuthentication().getUserName());
properties.setProperty("mail.smtp.auth", "true");

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

オーセンティケーターは次のようになります。

private class MyAuthenticator extends javax.mail.Authenticator 
{
    private PasswordAuthentication authentication;

    public Authenticator() 
    {
         String username = "auth-user";
         String password = "auth-password";
         authentication = new PasswordAuthentication(username, password);
    }

    protected PasswordAuthentication getPasswordAuthentication() 
    {
        return authentication;
    }
}

これはすべてテストされていませんが、それがあなたがしなければならないすべてだと思います。少なくともあなたを正しい道に導くはずです。

于 2011-09-24T18:33:32.683 に答える