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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to