5 Replies Latest reply: Feb 20, 2012 2:01 AM by Bill Shannon-Oracle RSS

    How to check whether a SMTP server is supporting Authentication or not

    SarojaKandepuneni
      Hi All,

      We are using Java Mail API 1.3.1/1.3.2 to send the messages. some of the SMTP servers that we use are supporting authentication and some of them are not.

      As the SMTPTransport.supportsAuthentication() is not available only in Java mail API 1.4.1, we are identifying the SMTP server whether it is supporting authentication or not in the following way.

      Socket clientSocket = null;
      InetSocketAddress socketAddress = null;
      OutputStream outStream = null;
      InputStream inStream = null;
      InputStreamReader inReader = null;
      OutputStreamWriter outWritter = null;


      try
      {

      clientSocket = new Socket();

      socketAddress = new InetSocketAddress(host, port);

      clientSocket.connect(socketAddress, timeout*1000); // convert timeout from second to miliseconds


      // 1: now try to execute the given command by passing that on Out-Socket

      outStream = clientSocket.getOutputStream();
      outWritter = new OutputStreamWriter(outStream);

      outWritter.write("ehlo localhost" +"\n");
      outWritter.flush();

      // 2:Read output of above command

      inStream = clientSocket.getInputStream();

      inReader = new InputStreamReader(inStream);

      // This array limit would be fine to get "AUTH" extension of smtp server.
      char[] arr = new char[16384];

      StringBuilder strBuilder = new StringBuilder();

      inReader.read(arr);

      for(int i=0; i< arr.length; i++)
      {
      strBuilder.append(arr);
      }


      System.out.println(METHOD_NAME + "SMTP server response for ehlo localhost command ->"+strBuilder.toString());
      // The output EHLO command comes like below :
      //
      // ehlo localhost
      // 250-ap9058pc.us.oracle.com Hello ap614ses.us.oracle.com [130.35.33.43], pleased to meet you
      // 250-ENHANCEDSTATUSCODES
      // 250-PIPELINING
      // 250-8BITMIME
      // 250-SIZE
      // 250-DSN
      // 250-ETRN
      // 250-AUTH GSSAPI DIGEST-MD5 CRAM-MD5 LOGIN PLAIN
      //

      // Since for XATUH ( like internal IP),
      // we are not sure, so just checking for AUTH capability.
      supported = strBuilder.indexOf("250-AUTH") >=0? true : false;

      }

      As shown in above code, we are issuing 'ehlo localhost' command to SMTP server, if the response i. 'strBuilder' contains '250-AUTH' then we are assuming that it is supporting authentication.

      But for one SMTP server the 'strBuilder' value is showing as '220 mail.durofelguera.com ESMTP Service (Lotus Domino Release 8.5.2) ready at Thu, 16 Feb 2012 13:57:20 +0100' only which is socket connection output but not 'ehlo localhost' command output.
      where as the telnet test output is showing correct only as below
      # telnet mail.durofelguera.com 25
      Trying 172.20.16.65...
      Connected to mail.durofelguera.com.
      Escape character is '^]'.
      220 mail.durofelguera.com ESMTP Service (Lotus Domino Release 8.5.2) ready
      at 0
      ehlo localhost
      250-mail.durofelguera.com Hello localhost ([172.20.15.209]), pleased to meet
      yu
      250-HELP
      250-AUTH LOGIN
      250-SIZE
      250 PIPELINING
      AUTH LOGIN

      The question is why the 'strBuilder' is not showing 'ehlo localhost' conad output where as the telnet test results are showing correctly, what is going wrong here?
      Is there any other way to check that whether SMTP server is supporting authentication or not?

      Edited by: sarojak on Feb 19, 2012 10:11 PM