Connect to Exchange Server

D

dezso.bolyan

Hello!

I need to connect to MS Exchange Server and get any email with
attechments. Use of IMAP and POP3 are not allowed on this Exchange
Server.

Have anybody any idea, how can I resolve this problem with JAVA?

Thanks a lot!
 
I

iksrazal

(e-mail address removed) escreveu:
Hello!

I need to connect to MS Exchange Server and get any email with
attechments. Use of IMAP and POP3 are not allowed on this Exchange
Server.

Have anybody any idea, how can I resolve this problem with JAVA?

Thanks a lot!

You could test via sockets to see if it least follows the RFC's. Might
be worth a quick shot. This code was used with the email server
'postfix' to prove a point about how to close sockets properly. No clue
about how to get attachments. Another suggestion is to google for
javamail and exchange - chances are someone has had the same problem.

package com.whitezone;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.nio.channels.SocketChannel;
import java.util.StringTokenizer;

// Test case code for file descriptor leak.
// The following should produce somewhere near 150 sockets in
CLOSE_WAIT state.
//
// The problem appears to be in
sun.nio.ch.SocketAdapter.SocketInputStream.read(ByteBuffer);
//

public class TestClose implements Runnable {

public static final String SMTP_HOSTNAME = "localhost";

public void run()
{
InetSocketAddress sockAddr = new
InetSocketAddress(SMTP_HOSTNAME, 25);
SocketChannel sChannel = null;
Socket socket = null;
try
{
sChannel = SocketChannel.open();
sChannel.connect(sockAddr);

// I've noticed that if I remove this line the problem no
longer happens
sChannel.socket().setSoTimeout(5000);

socket = sChannel.socket();

BufferedReader lineRdr = new BufferedReader(new
InputStreamReader(socket.getInputStream()));

String result = null;
do
{
// before performing the first readline the channel is
unregistered
System.err.println("before first
readline: isOpen =
"+sChannel.isOpen()+" isRegistered="+sChannel.isRegistered());

result = lineRdr.readLine();
System.err.println("<- "+result);

// after performing it is registered.
System.err.println("after first
readline: isOpen =
"+sChannel.isOpen()+" isRegistered="+sChannel.isRegistered());

} while(result != null && result.length() > 0
&&
result.matches("^[1-5][0-9]{2}-"));

if(result == null || result.length() == 0)
{
System.err.println("Received truncated
response from SMTP server "
+ sockAddr.getHostName());
return;
}

// Tokenize the last line result
//
StringTokenizer t = new
StringTokenizer(result);
int rc = Integer.parseInt(t.nextToken());
if(rc != 220) return;

//
// Send the QUIT command causing the server
side to close its end of
the connection
//
String cmd = "QUIT\r\n";
socket.getOutputStream().write(cmd.getBytes());
System.err.println("-> "+cmd);
do
{
result = lineRdr.readLine();
System.err.println("<- "+result);
} while(result != null && result.length() > 0
&&
result.matches("^[1-5][0-9]{2}-"));

if(result == null || result.length() == 0)
{
System.err.println("Received truncated
response from SMTP server "
+ sockAddr.getHostName());
return;
}

}
catch (Exception e) {
e.printStackTrace();
}
finally
{
try {

System.err.println("before close: isOpen =
"+sChannel.isOpen()+" isRegistered="+sChannel.isRegistered());

System.err.println("Closing SMTP socket channel
"+sChannel);
System.err.println("channel.socket().isConnected = "+
sChannel.socket().isConnected());
if (sChannel != null) {
//sChannel.close();
long start = System.currentTimeMillis();
System.currentTimeMillis();
Socket socket_close = sChannel.socket();
// by socket_close.shutdownOutput() itself,
CLOSE_WAIT issue fixed
socket_close.shutdownOutput();
// with socket_close.close() , isConnected
returns false sometimes
socket_close.close();
long end = System.currentTimeMillis();
System.err.println("Closed SMTP socket channel
"+sChannel+", Execution time was "+(end-start)+" ms.");

// The socket is still connected here.
System.err.println("\n\nAFTER shutdownOutput
channel.socket().isConnected = "+sChannel.socket().isConnected());
}
}
catch (Exception e) {
System.err.println("Exception on close:");
e.printStackTrace();
}
}

return;
}

public static void main(String[] args) {
TestClose test = new TestClose();
for(int i = 0; i < 150; i++) {
// this bug seems only to appear if different threads are
reading the channels
Thread thread = new Thread(test);
thread.start();
try {thread.join(); } catch(InterruptedException e) { }
}

System.err.println("Going to sleep.... run netstat -an | grep
CLOSE_WAIT ");
while(true) {
try { Thread.sleep(10000); } catch(InterruptedException
e) {}
}
}
}

HTH,
iksrazal
http://www.braziloutsource.com/
 
G

Gordon Beaton

(e-mail address removed) escreveu:
You could test via sockets to see if it least follows the RFC's.

Your example only tests whether the server supports SMTP, which is a
protocol for sending mail, not retrieving it.

Retrieving mail is generally done with POP or IMAP or, in the case of
MS Exchange, MAPI.

The OP should be looking for a library or tool that supports MAPI, or
a (probably reverse engineered) description of the protocol so he can
implement it himself.

/gordon
 
I

iksrazal

Gordon Beaton escreveu:
Your example only tests whether the server supports SMTP, which is a
protocol for sending mail, not retrieving it.

Retrieving mail is generally done with POP or IMAP or, in the case of
MS Exchange, MAPI.

The OP should be looking for a library or tool that supports MAPI, or
a (probably reverse engineered) description of the protocol so he can
implement it himself.

/gordon

Yeah, I meant to clarify that but lost power on my followup ;-).
Nevertheless, I meant to say that if you can send mail over vanilla
sockets you also can retrieve them via vanilla sockets, right? And if
you're going to reverse engineer MAPI, sockets are a great way because
of ethereal and the like as well as the flexibility.

Still of course Gordon is right. Unfortunately, according to Roedy
javamail doesn't support MAPI.

http://mindprod.com/jgloss/javamail.html

"JavaMail does not support the Windows proprietary MAPI protocol."

Good luck to the OP, might be a big challenge.
iksrazal
 
G

Gerbrand van Dieijen

(e-mail address removed) schreef:
Still of course Gordon is right. Unfortunately, according to Roedy
javamail doesn't support MAPI.

http://mindprod.com/jgloss/javamail.html

"JavaMail does not support the Windows proprietary MAPI protocol."

Good luck to the OP, might be a big challenge.
iksrazal

Best way seems to may to convince the adminstrator of the exchange
server to enable (secure) imap or pop3. Exchange does support that after
all (I could access exchange using netscape/thunderbird). Secure imap is
quite save too, especially when compared to MAPI.
 
N

Neil Padgen

Hello!

I need to connect to MS Exchange Server and get any email with
attechments. Use of IMAP and POP3 are not allowed on this Exchange
Server.

Have anybody any idea, how can I resolve this problem with JAVA?

Thanks a lot!

You could take a look at fetchExc. This is a Java program which grabs mail
from an Exchange server and forwards it to another destination. It depends
on having Outlook Web Access enabled on your Exchange server.

http://personal.inet.fi/atk/fetchexc/

I've used it in the past with reasonable success.

-- Neil
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top