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;
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");
// 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;
StringBuilder strBuilder = new StringBuilder();
for(int i=0; i< arr.length; i++)
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 [126.96.36.199], pleased to meet you
// 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
Connected to mail.durofelguera.com.
Escape character is '^]'.
220 mail.durofelguera.com ESMTP Service (Lotus Domino Release 8.5.2) ready
250-mail.durofelguera.com Hello localhost ([172.20.15.209]), pleased to meet
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
some of our customers are on Java Mail API 1.3.1 and 1.3.2, they cannot upgrade to 1.4.
Using Java Mail API 1.3.1 and 1.3.2, how can we check whether the SMTP server is supporting authentication or not?
They are on EBS 11i where Java Mail API 1.3.1 and 1.3.2 only certified.
Can anyone suggest how can we check whether a particular SMTP server is supporting authentication or not in java Mail API 1.3.1 or 1.3.2?
There are so many things wrong with your code, it's hard to know where to start...
Basically, the problem is not as simple as you think it is.
For example, some servers might not allow authentication until you've issued
the STARTTLS command.
These days, essentially all servers allow authentication. You're probably better
off just assuming the server supports.