IMAP/TLS Problem in connecting using java mail api.

Hi All,

I have a IMAP4 server running with TLS authentication switched on and have self signed certificate. I do not want to

install certificates so I tried DummySSLSocketFactory and DummyTrustManager provided by SUN but it gives following

error. Can anyone help me here?

DEBUG: setDebug:JavaMail version 1.3.3

DEBUG: getProvider() returning javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc]

DEBUG: mail.imap.fetchsize: 16384

javax.mail.MessagingException: Connection refused: connect;

nested exception is:

java.net.ConnectException: Connection refused: connect

at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:461)

at javax.mail.Service.connect(Service.java:236)

at javax.mail.Service.connect(Service.java:137)

at getmail.receiveEmails(getmail.java:40)

at getmail.main(getmail.java:18)

DEBUG: setDebug: JavaMail version 1.4ea

DEBUG: getProvider() returning javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc]

DEBUG: mail.imap.fetchsize: 16384

javax.mail.MessagingException: Unconnected sockets not implemented;

nested exception is:

java.net.SocketException: Unconnected sockets not implemented

at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:479)

at javax.mail.Service.connect(Service.java:275)

at javax.mail.Service.connect(Service.java:156)

at getmail.receiveEmails(getmail.java:27)

Here is my class.

import java.io.IOException;

import java.util.Properties;

import javax.mail.MessagingException;

import javax.mail.NoSuchProviderException;

import javax.mail.Session;

import javax.mail.Store;

public class getmail {

// myauth auth;

public static void main(String args[]) throws Exception {

getmail getmail = new getmail();

getmail.receiveEmails();

}

private void receiveEmails() throws NoSuchProviderException, MessagingException, IOException {

// String host, name, passwd;

String host = "HOST";

String name = "USER";

String passwd = "PASSWORD";

java.security.Security.setProperty("ssl.SocketFactory.provider", "DummySSLSocketFactory");

// Get a Properties object

Properties props = System.getProperties();

props.setProperty("mail.imaps.socketFactory.fallback", "false");

Session session = Session.getDefaultInstance(props, null);

session.setDebug(true);

Store store = session.getStore("imaps");

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);

}

}

Thanks in advance.

-Prasad

Message was edited by:

pchadhari

[2966 byte] By [pchadharia] at [2007-11-26 20:08:27]
# 1

There's a bug in the example DummySSLSocketFactory. You need to add this method:

public Socket createSocket() throws IOException {

return factory.createSocket();

}

bshannona at 2007-7-9 23:11:10 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2

Thanks for reply. After adding that method I am getting following exception

DEBUG: setDebug: JavaMail version 1.4ea

DEBUG: getProvider() returning javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc]

DEBUG: mail.imap.fetchsize: 16384

javax.mail.MessagingException: Connection refused: connect;

nested exception is:

java.net.ConnectException: Connection refused: connect

at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:479)

at javax.mail.Service.connect(Service.java:275)

at javax.mail.Service.connect(Service.java:156)

at GetMail.receiveEmails(GetMail.java:27)

at GetMail.main(GetMail.java:11)

pchadharia at 2007-7-9 23:11:10 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3
See the JavaMail FAQ for how to debug connection problems.
bshannona at 2007-7-9 23:11:10 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 4

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

pchadharia at 2007-7-9 23:11:10 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 5

Looks like Tomcat is not letting you establish a connection to the mail server. In the tomcat environment, does your program work with out TLS? Are you able to send an email from Tomcat to IMAP server (where security is turned off)?

If not it is not security related, it is connection related. Please make sure that you have the correct permissions granted:

grant {

// following two permissions allow

// access to default config files

permission java.io.FilePermission

"/path/to/mail.jar", "read";

permission java.io.FilePermission

"/path/to/activation.jar", "read";

// following to use SMTP

permission java.net.SocketPermission

"SMTPHOST:25", "connect,resolve";

// following to use IMAP

permission java.net.SocketPermission

"IMAPHOST:143", "connect,resolve";

// following to use POP3

permission java.net.SocketPermission

"POP3HOST:110", "connect,resolve";

// following needed if System.getProperties() is used

permission java.util.PropertyPermission

"*", "read,write";

};

Please see more details (FAQ):

http://java.sun.com/products/javamail/FAQ.html#securityManager

satishva at 2007-7-9 23:11:10 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 6

Thanks for your reply.

Security turned off is not an option not even for testing :-(

I tried to grant the permissions inside the the catalina.policy file inside tomcat with following lines.

grant {

permission java.io.FilePermission "file:${catalina.home}\webapps\my-app\WEB-INF\lib\mail-1.4.jar", "read";

permission java.io.FilePermission "file:${catalina.home}\webapps\my-app\WEB-INF\lib\java.activation-activation-1.1.jar", "read";

// following to use IMAP

permission java.net.SocketPermission "MY_HOST:143", "connect,resolve";

// following needed if System.getProperties() is used

permission java.util.PropertyPermission "*", "read,write";

};

Still same exception happens.

INFO: XML validation disabled

DEBUG: setDebug: JavaMail version 1.4ea

DEBUG: getProvider() returning javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc]

DEBUG: mail.imap.fetchsize: 16384

DEBUG: enable STARTTLS

* OK [CAPABILITY IMAP4REV1 LOGIN-REFERRALS STARTTLS LOGINDISABLED] MY_HOST IMAP4rev1 2003.339 at Tue, 6 Mar 2007 11:10:23 +0100 (CET)

A0 CAPABILITY

* CAPABILITY IMAP4REV1 IDLE NAMESPACE MAILBOX-REFERRALS BINARY UNSELECT SCAN SORT THREAD=REFERENCES THREAD=ORDEREDSUBJECT MULTIAPPEND LOGIN-REFERRALS STARTTLS L

OGINDISABLED

A0 OK CAPABILITY completed

DEBUG: protocolConnect login, host=MY_HOST, user=MY_USER, password=<non-null>

A1 STARTTLS

A1 OK STARTTLS completed

A2 CAPABILITY

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)

at org.springframework.scheduling.quartz.QuartzJobBean.execute(QuartzJobBean.java:90)

at org.quartz.core.JobRunShell.run(JobRunShell.java:203)

at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:520)

Caused by: com.sun.mail.iap.ProtocolException: * BYE JavaMail Exception: java.net.SocketException: Socket closed

at com.sun.mail.imap.protocol.IMAPProtocol.capability(IMAPProtocol.java:115)

at com.sun.mail.imap.IMAPStore.login(IMAPStore.java:492)

at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:458)

... 7 more

06.03.2007 11:10:23 org.apache.coyote.http11.Http11BaseProtocol start

Any clues?

Best Regads,

-Prasad

pchadharia at 2007-7-9 23:11:10 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 7

Looks like from the following debug message, login is disabled on the server. Is it possible that since you tried many times may be un-successfully with this user name, server disabled it? Please enable your user login and see the code works.

thanks

Satish

DEBUG: enable STARTTLS

* OK [CAPABILITY IMAP4REV1 LOGIN-REFERRALS STARTTLS LOGINDISABLED] MY_HOST IMAP4rev1 2003.339 at Tue, 6 Mar 2007 11:10:23 +0100 (CET)

A0 CAPABILITY

satishva at 2007-7-9 23:11:10 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 8

I learnt from Bill that LOGINDISABLED just means that the simple LOGIN command that sends plain text passwords is disabled. After STARTTLS the new CAPABILITIES will show that LOGIN is allowed.

Please turn on the SSL/TLS debugging options

(referenced in SSLNOTES.txt) and see if that provides any clues.

It's not a good sign that the first command after STARTTLS causes the

connection to drop. Bill suspects that something is going wrong with the TLS

communication.

http://java.sun.com/products/javamail/FAQ.html#ssldebug

satishva at 2007-7-9 23:11:10 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...