Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

IMAP /TLS standalone programm works fine, in Tomcat fails.

 
P Chaudhari
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I have a IMAP4 server running with TLS authentication switched on and have self signed certificate. do not want toinstall certificates so I tried DummySSLSocketFactory and DummyTrustManager provided by SUN.

Standalone programm works correctly. Please see code below. But If I put the code into Tomcat throws exception. Any clue?

private void receiveEmails() throws NoSuchProviderException, MessagingException, IOException {
// String host, name, passwd;
String host = "HOST_NAME";
String name = "USER_ID";
String passwd = "PASSWORD";
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
java.security.Security.setProperty("ssl.SocketFactory.provider", "DummySSLSocketFactory");
// Get a Properties object
Properties props = System.getProperties();
props.setProperty("mail.imap.socketFactory.fallback", "false");
props.setProperty("mail.imap.starttls.enable", "true");
Session session = Session.getDefaultInstance(props, null);
session.setDebug(false);
Store store = session.getStore("imap");
try {
store.connect(host, name, passwd); // exception here
System.out.println("store connected");
} catch (Exception e) {
// System.out.println("Error :" + e.toString());
e.printStackTrace();
System.exit(1);
}
}
---------------------------------------------------------------------------------------------

Inside Tomcat :- Same code in tomcat throws exception. Also it does not give call to DummySSLSocketFactory.

Code and Exception.

public void doExecute(JobExecutionContext context) {
String host = "lxeext12.deutsche-boerse.de";
String name = "capacity";
String passwd = "capacity";
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
Security.setProperty("ssl.SocketFactory.provider", com.deutscheboerse.comxerv.common.service.timer.DummySSLSocketFactory.class.getName());
// Get a Properties object
Properties props = System.getProperties();
props.setProperty("mail.imap.socketFactory.fallback", "false");
props.setProperty("mail.imap.starttls.enable", "true");

try {
Session session = Session.getDefaultInstance(props, null);
session.setDebug(true);
Store store = session.getStore("imap");
store.connect(host, name, passwd); // exception here
System.out.println("store connected");
}catch (Exception e) {
e.printStackTrace();
}
}



javax.mail.MessagingException: * BYE JavaMail Exception: java.net.SocketException: Socket closed;
nested exception is:
com.sun.mail.iap.ProtocolException: * BYE JavaMail Exception: java.net.SocketException: Socket closed
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:477)
at javax.mail.Service.connect(Service.java:275)
at javax.mail.Service.connect(Service.java:156)


--------------------------------------------------------------------------------

If I change the bold marked line below then.

public void doExecute(JobExecutionContext context) {
String host = "lxeext12.deutsche-boerse.de";
String name = "capacity";
String passwd = "capacity";
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
// Get a Properties object
Properties props = System.getProperties();
props.setProperty("mail.imap.socketFactory.class", com.deutscheboerse.comxerv.common.service.timer.DummySSLSocketFactory.class.getName());
props.setProperty("mail.imap.socketFactory.fallback", "false");
props.setProperty("mail.imap.starttls.enable", "true");

try {
Session session = Session.getDefaultInstance(props, null);
session.setDebug(true);
Store store = session.getStore("imap");
store.connect(host, name, passwd); // exception here
}catch (Exception e) {
e.printStackTrace();
}
}


- It calls the DummySSLSocketFactory but throws following exception.

Exception is:-
javax.mail.MessagingException: Unrecognized SSL message, plaintext connection?;
nested exception is:
javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connecti
on?
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:479)
at javax.mail.Service.connect(Service.java:275)
at javax.mail.Service.connect(S
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic