Java Check socks5还活着

时间:2015-01-07 13:35:07

标签: java email socks

我有一个通过Java发送电子邮件的程序。我尝试使用socks5连接到电子邮件帐户。

public class Read extends Task {

    final static org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(Read.class);

    public static void gmxSet(String[] buffer){

        try {
            int port = 587;
            String host = "mail.gmx.net";
            String user = buffer[0];
            String pwd = buffer[1];

            Properties properties = new Properties();
            File propertiesFile = new File("config.properties");
            if (propertiesFile.exists()) {
                BufferedInputStream bis = new BufferedInputStream(new FileInputStream(propertiesFile));
                properties.load(bis);
                bis.close();
            }

            String checkSocks = "socks.txt";
            File file = new File(checkSocks);
            BufferedReader reader = null;
            reader = new BufferedReader(new FileReader(file));
            String zeile = reader.readLine();
            String[] buffer1 = new String[0];

            List<String> lines = new ArrayList<String>();

            while (zeile != null) {
                lines.add(zeile);
                zeile = reader.readLine();
            }
            Random r = new Random();

                String randomString = lines.get(r.nextInt(lines.size()));
                buffer1 = randomString.split(";");


            String username = buffer1[2];
            String password = buffer1[3];
            String hostS = buffer1[0];
            String portS = buffer1[1];

            Properties props = new Properties();
            props.put("mail.smtp.starttls.enable", "true");
            props.put("mail.smtp.auth", "true");
            if (email.Email.SocksAnAus.isSelected()) {
                java.net.Authenticator authenticator = new java.net.Authenticator() {

                    protected java.net.PasswordAuthentication getPasswordAuthentication() {
                        return new java.net.PasswordAuthentication(username, password.toCharArray());
                    }
                };

                props.put("mail.smtp.socks.host", hostS);
                props.put("mail.smtp.socks.port", portS);
                System.setProperty("java.net.socks.username", username);
                System.setProperty("java.net.socks.password", password);
                java.net.Authenticator.setDefault(authenticator);


            }


            props.put("mail.smtp.starttls.enable", "true");
            props.put("mail.smtp.auth", "true");

            Session session = Session.getInstance(props, null);
            Transport transport = session.getTransport("smtp");
            transport.connect(host, port, user, pwd);
            transport.close();



        } catch (MessagingException ex) {
log.error("There was an error : " + ex);

        } catch (IOException ex) {
            log.error("There was an error : " + ex);

        }
    }

    public Read(File file) {
        super(file);
    }
}

工作正常。

现在有时候我的socks5离线,因为我有一个列表,不止一个socks5。

我在使用socks5之前如何检查socks5是否活着?

例如。

if ( buffer1.isAlive ){
    do work 
}else{
    delete socks from txt file and try another one ( I can do it later )
}

0 个答案:

没有答案