473,395 Members | 1,613 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 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 3963

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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.