You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by jamieb <ja...@fastmail.fm> on 2008/03/12 18:13:08 UTC
Java Mail Inside Tomcat
Hi there
I have encountered a bizzare problem... I have a small peice of code that
fetches emails from an IMAP server using TLS. This code works perfectly in a
standalone application, however, when I copy and paste it over to a Tomcat
application it does not work. The code uses Java Mail 1.4.1 and Java Secure
Sockets.
The following error is outputted when the code is run from within Tomcat:
javax.mail.MessagingException: Unrecognized SSL message, plaintext
connection?;
nested exception is:
javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
NOTE: in case you are wondering: YES in both cases the server is connecting
to the same port.
Now I have checked:
- the same java mail version is used across applications
- the same JRE (v1.6)
- all input parameters are the same
Any ideas on what might be causing this problem in the Tomcat environment?
package com.test.support;
import java.io.*;
import java.security.Security;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
import java.net.*;
public class TestMailboxConnection {
/**
* @param args
*/
private static final String DUMMY_SSL_FACTORY =
"com.test.support.DummySSLSocketFactory";
public static void main(String[] args) {
if (args.length<3) {
System.out.println("\n\nUtility to Detect Mail Server
Connection Settings");
System.out.println("Usage: TestMailboxConnection server
username password port secure_port ");
return;
}
String server = args[0];
String username = args[1];
String password = args[2];
String port = "143";
String secureport = "993";
if (args.length>3) {
port = args[3];
secureport = args[4];
}
Properties props = new Properties();
// Insecure Test
testEcho(server,port);
//testEcho(server,secureport);
System.out.println("properties:"+props);
String protocol = "imap";
props.put("mail."+protocol+".port",port);
test("imap
insecure",protocol,server,Integer.valueOf(port),username,password,props);
props.put("mail."+protocol+".starttls.enable", Boolean.TRUE);
props.put("mail."+protocol+".socketFactory.fallback","true");
props.put("mail."+protocol+".socketFactory.class",
getSSLFactory());
props.put("mail."+protocol+".socketFactory.port",secureport);
test("imap tls
(fallback)",protocol,server,Integer.valueOf(port),username,password,props);
props.put("mail."+protocol+".socketFactory.fallback","false");
test("imap
tls",protocol,server,Integer.valueOf(port),username,password,props);
protocol = "imaps";
props = new Properties();
props.put("mail."+protocol+".socketFactory.fallback","false");
props.put("mail."+protocol+".socketFactory.class",
getSSLFactory());
props.put("mail."+protocol+".socketFactory.port",secureport);
test("imap
ssl",protocol,server,Integer.valueOf(port),username,password,props);
}
public static String getSSLFactory() {
return DUMMY_SSL_FACTORY;
}
public static void test(String testName, String protocol, String server,
int port, String username, String password, Properties props) {
java.security.Provider[] providers = Security.getProviders();
Session session = Session.getInstance(props, null);
session.setDebug(true);
Store store = null;
try {
store = session.getStore(protocol);
} catch (Exception nspe) {
System.out.println("no such provider");
return;
}
try {
System.out.println("\nprotocol='"+protocol+"',server='"+server+"',port='"+port+"',username='"+username+"',password='"+password+"'}");
System.out.println(props+"\n");
store.connect(server,Integer.valueOf(port), username,password);
} catch (Exception e) {
System.out.println("\n>>>>>>>>>>>>>>>>>
failed:"+e.getMessage()+"\n");
System.out.println("mailbox connection properties "+props);
e.printStackTrace();
return;
}
System.out.println("\n>>>>>>>>>>>>>>>>> success!"+"\n");
System.out.println("mailbox connection properties "+props);
return;
}
public static void testEcho(String server,String port) {
System.out.println("test echo (port "+port+"):");
BufferedReader in = null;
Socket echoSocket=null;
try {
echoSocket = new Socket(server, Integer.valueOf(port));
in = new BufferedReader(new InputStreamReader(
echoSocket.getInputStream()));
echoSocket.setSoTimeout(2000);
System.out.println("echo: " + in.readLine());
in.close();
echoSocket.close();
} catch (UnknownHostException e) {
System.err.println("unknown host host:"+server);
return;
} catch (IOException e) {
System.err.println("IO error occurred while connecting to
host:"+e.getMessage());
return;
}
}
}
--
View this message in context: http://www.nabble.com/Java-Mail-Inside-Tomcat-tp16008995p16008995.html
Sent from the Tomcat - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
Re: Java Mail Inside Tomcat
Posted by jamieb <ja...@fastmail.fm>.
Hi Sameer
Thanks for the pointer. As it happened, I managed to get StartTLS to work
inside Tomcat, although I am still not sure why there is a difference in
behaviour when the below application is run standalone vs inside Tomcat. In
advance, please forgive the extended explanation but I am acutely aware that
this may information may help someone else in the event they come across the
same problem.
What I discovered was (what seems like) a limitation in the Java mail API:
o I was attempting to use a DummySSLFactory to skip certificate
authentication
o Java mail only allows you to specify one socketFactoryClass i.e.
mail.imap.socketFactory.class
o When you are using StartTLS, Java mail should be using two socketFactory
classes (one for the first plaintext connection and then another for the
subsequent secure connection)
o Javamail does not provide a way to set two different socket factory
classes
o setting the default ssl socket factory using the SSL libraries directly
had no effect inside Tomcat
In the end, I ended up editing the Java mail source code and changing
com.sun.mail.util.SocketFetcher
the Java mail source code to accept: mail.imap.startTLS.socketFactory.class.
public static Socket startTLS(Socket socket, Properties props,
String prefix) throws IOException {
....
props.getProperty(prefix + ".startTLS.socketFactory.class", null);
....
}
That way, I could now specify:
mail.imap.socketFactory.class and mail.imap.startTLS.socketFactory.class so
that different Socket factories would be called depending on whether a
plaintext or encrypted connection is being negotiated as part of the
StartTLS protocol extension.
At this point, I am wondering whether I am missing something about the use
of the StartTLS functionality or this is a genuine issue in the java mail
API.
Thanks for the time taken to investigate this problem. I really appreciate
it!
Regards,
Jamie
Sameer Acharya wrote:
>
> Can you give the line number as to where the error is
> happening.
> I found this link on google search you can read thru
> the same it describes similar problems.
> We have not had problems using java mail under tomcat
> but we dont use imap.
> http://forum.java.sun.com/thread.jspa?threadID=761635&messageID=4347363
>
> -Sameer
>
> --- jamieb <ja...@fastmail.fm> wrote:
>
>>
>>
>> Hi there
>>
>> I have encountered a bizzare problem... I have a
>> small peice of code that
>> fetches emails from an IMAP server using TLS. This
>> code works perfectly in a
>> standalone application, however, when I copy and
>> paste it over to a Tomcat
>> application it does not work. The code uses Java
>> Mail 1.4.1 and Java Secure
>> Sockets.
>>
>> The following error is outputted when the code is
>> run from within Tomcat:
>>
>> javax.mail.MessagingException: Unrecognized SSL
>> message, plaintext
>> connection?;
>> nested exception is:
>> javax.net.ssl.SSLException: Unrecognized SSL
>> message, plaintext connection?
>>
>> NOTE: in case you are wondering: YES in both cases
>> the server is connecting
>> to the same port.
>>
>> Now I have checked:
>> - the same java mail version is used across
>> applications
>> - the same JRE (v1.6)
>> - all input parameters are the same
>>
>> Any ideas on what might be causing this problem in
>> the Tomcat environment?
>>
>> package com.test.support;
>>
>> import java.io.*;
>> import java.security.Security;
>> import java.util.Properties;
>> import javax.mail.*;
>> import javax.mail.internet.*;
>> import java.net.*;
>>
>> public class TestMailboxConnection {
>>
>> /**
>> * @param args
>> */
>>
>> private static final String DUMMY_SSL_FACTORY =
>> "com.test.support.DummySSLSocketFactory";
>>
>> public static void main(String[] args) {
>>
>>
>> if (args.length<3) {
>> System.out.println("\n\nUtility to
>> Detect Mail Server
>> Connection Settings");
>> System.out.println("Usage:
>> TestMailboxConnection server
>> username password port secure_port ");
>> return;
>> }
>> String server = args[0];
>> String username = args[1];
>> String password = args[2];
>>
>> String port = "143";
>> String secureport = "993";
>>
>> if (args.length>3) {
>> port = args[3];
>> secureport = args[4];
>> }
>>
>>
>> Properties props = new Properties();
>> // Insecure Test
>> testEcho(server,port);
>> //testEcho(server,secureport);
>> System.out.println("properties:"+props);
>> String protocol = "imap";
>> props.put("mail."+protocol+".port",port);
>> test("imap
>>
> insecure",protocol,server,Integer.valueOf(port),username,password,props);
>>
>> props.put("mail."+protocol+".starttls.enable",
>> Boolean.TRUE);
>>
>>
> props.put("mail."+protocol+".socketFactory.fallback","true");
>>
>> props.put("mail."+protocol+".socketFactory.class",
>> getSSLFactory());
>>
>>
> props.put("mail."+protocol+".socketFactory.port",secureport);
>>
>> test("imap tls
>>
> (fallback)",protocol,server,Integer.valueOf(port),username,password,props);
>>
>>
> props.put("mail."+protocol+".socketFactory.fallback","false");
>> test("imap
>>
> tls",protocol,server,Integer.valueOf(port),username,password,props);
>>
>> protocol = "imaps";
>> props = new Properties();
>>
>>
> props.put("mail."+protocol+".socketFactory.fallback","false");
>>
>> props.put("mail."+protocol+".socketFactory.class",
>> getSSLFactory());
>>
>>
> props.put("mail."+protocol+".socketFactory.port",secureport);
>> test("imap
>>
> ssl",protocol,server,Integer.valueOf(port),username,password,props);
>>
>>
>>
>> }
>>
>> public static String getSSLFactory() {
>> return DUMMY_SSL_FACTORY;
>> }
>>
>> public static void test(String testName, String
>> protocol, String server,
>> int port, String username, String password,
>> Properties props) {
>> java.security.Provider[] providers =
>> Security.getProviders();
>> Session session = Session.getInstance(props,
>> null);
>> session.setDebug(true);
>> Store store = null;
>> try {
>> store = session.getStore(protocol);
>> } catch (Exception nspe) {
>> System.out.println("no such provider");
>> return;
>> }
>> try {
>>
>>
> System.out.println("\nprotocol='"+protocol+"',server='"+server+"',port='"+port+"',username='"+username+"',password='"+password+"'}");
>> System.out.println(props+"\n");
>> store.connect(server,Integer.valueOf(port),
>> username,password);
>> } catch (Exception e) {
>> System.out.println("\n>>>>>>>>>>>>>>>>>
>> failed:"+e.getMessage()+"\n");
>> System.out.println("mailbox connection
>> properties "+props);
>> e.printStackTrace();
>> return;
>> }
>> System.out.println("\n>>>>>>>>>>>>>>>>>
>> success!"+"\n");
>> System.out.println("mailbox connection
>> properties "+props);
>> return;
>> }
>>
>> public static void testEcho(String server,String
>> port) {
>> System.out.println("test echo (port "+port+"):");
>> BufferedReader in = null;
>> Socket echoSocket=null;
>> try {
>> echoSocket = new Socket(server,
>> Integer.valueOf(port));
>> in = new BufferedReader(new
>> InputStreamReader(
>>
>> echoSocket.getInputStream()));
>> echoSocket.setSoTimeout(2000);
>> System.out.println("echo: " +
>> in.readLine());
>> in.close();
>> echoSocket.close();
>> } catch (UnknownHostException e) {
>> System.err.println("unknown host
>> host:"+server);
>> return;
>> } catch (IOException e) {
>> System.err.println("IO error occurred
>> while connecting to
>> host:"+e.getMessage());
>> return;
>> }
>>
>>
>> }
>>
>>
>>
>> }
>>
>>
>> --
>> View this message in context:
>>
> http://www.nabble.com/Java-Mail-Inside-Tomcat-tp16008995p16008995.html
>> Sent from the Tomcat - User mailing list archive at
>> Nabble.com.
>>
>>
>>
> ---------------------------------------------------------------------
>> To start a new topic, e-mail:
>> users@tomcat.apache.org
>> To unsubscribe, e-mail:
>> users-unsubscribe@tomcat.apache.org
>> For additional commands, e-mail:
>> users-help@tomcat.apache.org
>>
>>
>
>
>
>
> ____________________________________________________________________________________
> Be a better friend, newshound, and
> know-it-all with Yahoo! Mobile. Try it now.
> http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ
>
>
> ---------------------------------------------------------------------
> To start a new topic, e-mail: users@tomcat.apache.org
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>
>
--
View this message in context: http://www.nabble.com/Java-Mail-Inside-Tomcat-tp16008995p16064463.html
Sent from the Tomcat - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
Re: Java Mail Inside Tomcat
Posted by Sameer Acharya <ac...@yahoo.com>.
Can you give the line number as to where the error is
happening.
I found this link on google search you can read thru
the same it describes similar problems.
We have not had problems using java mail under tomcat
but we dont use imap.
http://forum.java.sun.com/thread.jspa?threadID=761635&messageID=4347363
-Sameer
--- jamieb <ja...@fastmail.fm> wrote:
>
>
> Hi there
>
> I have encountered a bizzare problem... I have a
> small peice of code that
> fetches emails from an IMAP server using TLS. This
> code works perfectly in a
> standalone application, however, when I copy and
> paste it over to a Tomcat
> application it does not work. The code uses Java
> Mail 1.4.1 and Java Secure
> Sockets.
>
> The following error is outputted when the code is
> run from within Tomcat:
>
> javax.mail.MessagingException: Unrecognized SSL
> message, plaintext
> connection?;
> nested exception is:
> javax.net.ssl.SSLException: Unrecognized SSL
> message, plaintext connection?
>
> NOTE: in case you are wondering: YES in both cases
> the server is connecting
> to the same port.
>
> Now I have checked:
> - the same java mail version is used across
> applications
> - the same JRE (v1.6)
> - all input parameters are the same
>
> Any ideas on what might be causing this problem in
> the Tomcat environment?
>
> package com.test.support;
>
> import java.io.*;
> import java.security.Security;
> import java.util.Properties;
> import javax.mail.*;
> import javax.mail.internet.*;
> import java.net.*;
>
> public class TestMailboxConnection {
>
> /**
> * @param args
> */
>
> private static final String DUMMY_SSL_FACTORY =
> "com.test.support.DummySSLSocketFactory";
>
> public static void main(String[] args) {
>
>
> if (args.length<3) {
> System.out.println("\n\nUtility to
> Detect Mail Server
> Connection Settings");
> System.out.println("Usage:
> TestMailboxConnection server
> username password port secure_port ");
> return;
> }
> String server = args[0];
> String username = args[1];
> String password = args[2];
>
> String port = "143";
> String secureport = "993";
>
> if (args.length>3) {
> port = args[3];
> secureport = args[4];
> }
>
>
> Properties props = new Properties();
> // Insecure Test
> testEcho(server,port);
> //testEcho(server,secureport);
> System.out.println("properties:"+props);
> String protocol = "imap";
> props.put("mail."+protocol+".port",port);
> test("imap
>
insecure",protocol,server,Integer.valueOf(port),username,password,props);
>
> props.put("mail."+protocol+".starttls.enable",
> Boolean.TRUE);
>
>
props.put("mail."+protocol+".socketFactory.fallback","true");
>
> props.put("mail."+protocol+".socketFactory.class",
> getSSLFactory());
>
>
props.put("mail."+protocol+".socketFactory.port",secureport);
>
> test("imap tls
>
(fallback)",protocol,server,Integer.valueOf(port),username,password,props);
>
>
props.put("mail."+protocol+".socketFactory.fallback","false");
> test("imap
>
tls",protocol,server,Integer.valueOf(port),username,password,props);
>
> protocol = "imaps";
> props = new Properties();
>
>
props.put("mail."+protocol+".socketFactory.fallback","false");
>
> props.put("mail."+protocol+".socketFactory.class",
> getSSLFactory());
>
>
props.put("mail."+protocol+".socketFactory.port",secureport);
> test("imap
>
ssl",protocol,server,Integer.valueOf(port),username,password,props);
>
>
>
> }
>
> public static String getSSLFactory() {
> return DUMMY_SSL_FACTORY;
> }
>
> public static void test(String testName, String
> protocol, String server,
> int port, String username, String password,
> Properties props) {
> java.security.Provider[] providers =
> Security.getProviders();
> Session session = Session.getInstance(props,
> null);
> session.setDebug(true);
> Store store = null;
> try {
> store = session.getStore(protocol);
> } catch (Exception nspe) {
> System.out.println("no such provider");
> return;
> }
> try {
>
>
System.out.println("\nprotocol='"+protocol+"',server='"+server+"',port='"+port+"',username='"+username+"',password='"+password+"'}");
> System.out.println(props+"\n");
> store.connect(server,Integer.valueOf(port),
> username,password);
> } catch (Exception e) {
> System.out.println("\n>>>>>>>>>>>>>>>>>
> failed:"+e.getMessage()+"\n");
> System.out.println("mailbox connection
> properties "+props);
> e.printStackTrace();
> return;
> }
> System.out.println("\n>>>>>>>>>>>>>>>>>
> success!"+"\n");
> System.out.println("mailbox connection
> properties "+props);
> return;
> }
>
> public static void testEcho(String server,String
> port) {
> System.out.println("test echo (port "+port+"):");
> BufferedReader in = null;
> Socket echoSocket=null;
> try {
> echoSocket = new Socket(server,
> Integer.valueOf(port));
> in = new BufferedReader(new
> InputStreamReader(
>
> echoSocket.getInputStream()));
> echoSocket.setSoTimeout(2000);
> System.out.println("echo: " +
> in.readLine());
> in.close();
> echoSocket.close();
> } catch (UnknownHostException e) {
> System.err.println("unknown host
> host:"+server);
> return;
> } catch (IOException e) {
> System.err.println("IO error occurred
> while connecting to
> host:"+e.getMessage());
> return;
> }
>
>
> }
>
>
>
> }
>
>
> --
> View this message in context:
>
http://www.nabble.com/Java-Mail-Inside-Tomcat-tp16008995p16008995.html
> Sent from the Tomcat - User mailing list archive at
> Nabble.com.
>
>
>
---------------------------------------------------------------------
> To start a new topic, e-mail:
> users@tomcat.apache.org
> To unsubscribe, e-mail:
> users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail:
> users-help@tomcat.apache.org
>
>
____________________________________________________________________________________
Be a better friend, newshound, and
know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ
---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org