Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2005

Tip: Looking for answers? Try searching our database.

Connect to Exchange Server

Thread view: 
dezso.bolyan@gmail.com - 19 Dec 2005 12:43 GMT
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!
iksrazal@gmail.com - 19 Dec 2005 13:46 GMT
> Hello!
>
[quoted text clipped - 5 lines]
>
> 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/
Gordon Beaton - 19 Dec 2005 14:12 GMT
>> 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.

> 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

Signature

[  do not email me copies of your followups  ]
g o r d o n + n e w s @  b a l d e r 1 3 . s e

iksrazal@gmail.com - 19 Dec 2005 17:01 GMT
> >> 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
[quoted text clipped - 13 lines]
>
> /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
Gerbrand van Dieijen - 19 Dec 2005 19:36 GMT
iksrazal@gmail.com schreef:

> Still of course Gordon is right. Unfortunately, according to Roedy
> javamail doesn't support MAPI.
[quoted text clipped - 5 lines]
> 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.
Neil Padgen - 20 Dec 2005 11:56 GMT
> Hello!
>
[quoted text clipped - 5 lines]
>
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.