472,805 Members | 1,104 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,805 software developers and data experts.

Socket with IMAP server troubles :)

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
0 3917

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Sven Groot | last post by:
I have a Windows Service application that acts as if it's an SMTP server. Outlook connects to this service, which is always running on the localhost. This works fine most of the time. However,...
3
by: chirag | last post by:
i want a IMAP server for windows so that i can retrive incoming mails. I use mssql server to store user information. i need IMAP server to develop project i.e mail system can any body sent me...
4
by: Frank Meng | last post by:
Hi. I am trying a csharp sample from http://www.codeproject.com/csharp/socketsincs.asp . (Sorry I didn't post all the source codes here, please get the codes from above link if you want to try)....
1
by: EppO | last post by:
I wrote a small server/client app. I created a class for my customized socket object using (synchronous) Sockets functions for both server and client use. When client connects to server, if It...
0
by: Irmen de Jong | last post by:
I'm having some troubles with closing sockets using Python 2.5b1 Simply closing a client socket (on the server side) doesn't seem to actually shutdown the socket anymore. A connected client simply...
0
by: contactme | last post by:
Hi, Is it possible to open concurrent connections using Net::IMAP::Simple library ? My IMAP server allows 4 connections per ip, so I am having following problems while using Net::IMAP::Simple and...
11
by: mp- | last post by:
I want to be able to allow people to check their email from my PHP online application. Given only the users 1) email address, 2) username (if applicable) and 3) password - how can I auto detect...
2
by: pschulz | last post by:
Dear All, I am new in this forum and also relatively new to .Net. I need to create an application which needs to be able to act as a network server and provide data and messages to a variety of...
3
by: ros | last post by:
Hi! I have a simple PHP script: $mbox = imap_open ("{localhost:993/imap/ssl}INBOX", "test@domain.com", "123"); if(!$mbox) { echo 'Error: '.imap_last_error().'<br/>'; } Script works fine...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.