By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,276 Members | 1,951 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,276 IT Pros & Developers. It's quick & easy.

Socket with IMAP server troubles :)

P: n/a
Hello there,

I'm trying to create a connection with an IMAP server which is secured
through SSL. I can get the connection just fine, and I get a first respone
from the server. But then I send out my login string, and it does not seem
to arrive at the server, because I don't get an answer returned! After some
time, I receive a message from the server saying that I've been idle for too
long, and then the connection gets closed..
But why doesn't my data arrive at the server? (because I believe that 's the
problem here).

I realy hope someone could help me out :)
CheckIMAP.java:
-------------------
import java.io.*;
import java.net.*;
import javax.net.*;
import javax.net.ssl.*;
import java.security.*;

public class CheckIMAP
{
static Socket imap = null;
static OutputStreamWriter outStream = null;
static InputStream inStream = null;
static BufferedReader reader = null;

static int currentState = 0; /* 0 = nothing, 1=connecting, 2= login */

//public CheckIMAP(String host, String user, String password, int port)
public static void main(String[] args)
{
SSLContext sc = null;

String line = null;

// server information
String host = "server";
int port = 993;

try
{

// Create a trust manager that does not validate certificate
chains
TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
public java.security.cert.X509Certificate[]
getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(
java.security.cert.X509Certificate[] certs, String
authType) {
}
public void checkServerTrusted(
java.security.cert.X509Certificate[] certs, String
authType) {
}
}
};

// Install the all-trusting trust manager
try
{
sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new
java.security.SecureRandom());
}
catch (Exception e)
{
e.printStackTrace();
}

// connect with the IMAP server
currentState = 1;
SocketFactory socketFactory = sc.getSocketFactory();
imap = socketFactory.createSocket(host, port);

outStream = new OutputStreamWriter(imap.getOutputStream());
inStream = imap.getInputStream();
reader = new BufferedReader(new
java.io.InputStreamReader(inStream));

line = reader.readLine();
while(line != null)
{
print(line);
System.out.println("** CurrentState: "+currentState);
readData(line);
line = reader.readLine();
}

close();

}
catch(UnknownHostException e)
{
System.out.println("UnknownHostException has been caught.");
e.printStackTrace();
}
catch(IOException e)
{
System.out.println("IOException has been caught");
e.printStackTrace();
}

}

public static void readData(String line)
{

if( line.substring(0,4).equalsIgnoreCase("* OK") )
{
// connection ok, now authenticate
System.out.println("** Connected to IMAP server");
write("A001 LOGIN foo bar");
currentState = 2;
}
else
{
// Connection failed, abort
System.out.println("** Connection failed, aborting");
close();
}

}

// close the socket and all the streams
public static void close()
{
try
{
System.out.println("** Closing all connections.");
inStream.close();
outStream.close();
imap.close();
reader.close();
}
catch(IOException e)
{
System.out.println("IOException has been caught");
e.printStackTrace();
}
}

// sent a command to the server
public static void write(String cmd)
{
try
{
outStream.write(cmd);
outStream.flush();
}
catch(IOException e)
{
System.out.println("IOException has been caught");
e.printStackTrace();
}
System.out.println("--> "+cmd);
}

// print out every line received from the server
public static void print(String rcvd)
{
System.out.println("<-- "+rcvd);
}
}
Many thanks for any help!!

Steven
----------------------------------------------------------------------------
"Radio for me is to bring NEW music to people who then
can make their own selection for their personal use. It's
the beauty of this medium we call radio that is often
misused for commercial reasons in my opinion." - Armin van Buuren
Jul 18 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.