palimpsestor palimpsestor - 6 months ago 107
Java Question

Connection refused when using Java Mail to connect to IMAP

I have an IMAP server running locally (in a Docker container). I can connect to it via telnet as shown below, but when I try to connect to the same server from a Java program, I get a

java.net.ConnectException
. Why?

Here's the telnet session:

$ telnet 192.168.99.100 3143
Trying 192.168.99.100...
Connected to 192.168.99.100.
Escape character is '^]'.
* OK IMAP4rev1 Server GreenMail ready
a1 LOGIN testuser testpassword
a1 OK LOGIN completed.
a2 LIST "" "*"
* LIST () "." "INBOX"
a2 OK LIST completed.
a3 EXAMINE INBOX
* FLAGS (\Answered \Deleted \Draft \Flagged \Seen)
* 0 EXISTS
* 0 RECENT
* OK [UIDVALIDITY 1464494793709]
* OK No messages unseen
* OK [PERMANENTFLAGS (\Answered \Deleted \Draft \Flagged \Seen)]
a3 OK [READ-ONLY] EXAMINE completed.
a4 LOGOUT
* BYE IMAP4rev1 Server logging out
a4 OK LOGOUT completed.
Connection closed by foreign host.


Here's the sample Java code:

public class ImapTest {

public static void main(String[] args) {

Properties props = new Properties();
props.setProperty("mail.store.protocol", "imap");

Session imapSession = Session.getInstance(props);

try {
Store store = imapSession.getStore("imap");

store.connect("192.168.99.100", 3413, "testuser", "testpassword");
Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_ONLY);
}
catch (Exception e) {
e.printStackTrace();
}
}
}


And here's the resulting stackrace:

javax.mail.MessagingException: Connection refused;
nested exception is:
java.net.ConnectException: Connection refused
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:618)
at javax.mail.Service.connect(Service.java:291)
at test.ImapTest.main(ImapTest.java:23)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at java.net.Socket.connect(Socket.java:538)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:284)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:227)
at com.sun.mail.iap.Protocol.<init>(Protocol.java:109)
at com.sun.mail.imap.protocol.IMAPProtocol.<init>(IMAPProtocol.java:104)
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:585)
... 7 more

Answer

Seems to simply be a typo. Your telnet command tries to connect to port 3143 while your Java program connects to port 3413. Not the same port. Try changing your Java program to connect to 3143 which seems to be the port your IMAP server is listening on.

Comments