Java validate SMTP server using telnet

426 Views Asked by At

i'm using a library for validating SMTP connection to test invalid mail domains to avoid sending mails and bounce, simply i'm using telnet to simulate MAIL FROM: <{Address}> and RCPT TO: <{Address}> using BufferedWriter and BufferedReader as follows:

    private static void send(BufferedWriter wr, String text)
        throws IOException {
    wr.write(text + "\r\n");
    wr.flush();
    return;
}

    private static int receive(BufferedReader in) throws IOException {
    String line = null;
    int eCode = 0;
    while ((line = in.readLine()) != null) {
        String pfx = line.substring(0, 3);
        try {
            eCode = Integer.parseInt(pfx);
        } catch (Exception ex) {
            eCode = -1;
        }
        if (line.charAt(3) != '-') break;
    }
    return eCode;
}

calling just as follows:

 Socket skt = new Socket("gmail-smtp-in.l.google.com", 25);
 BufferedReader bufferedReader = new BufferedReader (new InputStreamReader(skt.getInputStream()));
 BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(skt.getOutputStream()));
 send(bufferedWriter, "MAIL FROM: <{Address}>");
 int status = receive(bufferedReader);
 send(bufferedWriter, "RCPT TO: <{Address}>");
 status = receive(bufferedReader);

the only problem i'm facing with this approach is performance, as MAIL FROM CMD typically takes 10 sec and RCPT TO CMD takes 20 sec (on terminal) which is too much, is there's an idea of how to do that fast or use another mechanism to achieve that correctly in 1 or 2 seconds maximum ?

0

There are 0 best solutions below