Solved

HTTPS : Download Attachments from MS Exchange Server

Posted on 2004-04-24
37
720 Views
Last Modified: 2007-12-19
Hi All,

I just started a small project where i need to communicate with MS Exchange Server . For this I  choose Apache Slide Project and working fine  .

But when i try to download the attachments from the Exchange server which is configured to HTTPS protocol, I am unable  to do it so because webDavResourceFile.getMethodData() is returning any empty input stream i.e. webDavResourceFile.getMethodData().available() is returning always a zero for me. So obviously i am not able to download
them

The Same thing is working if the server is configured to HTTP Protocol.

So please help me to get the attachments when it is configured to HTTPS protocol

thanks
sudhakar
0
Comment
Question by:sudhakar_koundinya
  • 23
  • 8
  • 6
37 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 10906903
Can't you point us to the online javadoc for the API you're using?
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 10906923
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 10906926
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10906957
Actually, although this is not intended to represent a solution to the problem, you should not use available() as it is unreliable. You should simply read the stream until it is exhausted.

What kind of resource is it that you're trying to get, as a matter of interest?
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 10906967
here is code snippet how i am working with it


      FileOutputStream attachmentOutStream = new FileOutputStream(
          strAttachmentFile);
      int offset = 0;
      //read the buffer from http stream
      while (true) {
        int availByteCount = attachmentInStream.available();

        if (availByteCount <= 0) {
          //end of attachment buffer
          break;
        }
        byte buffer[] = new byte[availByteCount];
        offset = attachmentInStream.read(buffer);
        //write the downloaded attachment buffer in the Local System Object
        attachmentOutStream.write(buffer, 0, offset);
        buffer = null;
      }
      //clear the buffer;
      attachmentOutStream.close();
      attachmentInStream.close();
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 10906970
as i said earlier

it is working with HTTP protocol, i am facing problem only with https protocol
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 10906975
and for getting the url stream

 org.apache.webdav.lib.WebdavResource webDavResourceFile = getWebResource(
          urlEML);

      //get the attachment
      InputStream attachmentInStream = webDavResourceFile.getMethodData();
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10906986
Are you sure this API supports https?
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 10906991
yes

because i am able to download all the emails and user acoount information using https protocol.  only i am facing with attachments of emails :(
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 10906993
and for creating the HTTPS URL Object
here is the snippet
url = new org.apache.commons.httpclient.HttpsURL(username, password, hostname, port, strPath)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10907000
Can you show me the value of urlEML? (disguise as little as possible)
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 10907003
     org.apache.commons.httpclient.HttpURL urlEML = getURL(path + "/" +
          mailbox + "/" + folder + "/" + filename);


  private org.apache.commons.httpclient.HttpURL getURL(String strPath) {
    org.apache.commons.httpclient.HttpURL url = null;
    try {
      if (bHttps == false) {
        //instantiate the email object for http protocol
        url = new org.apache.commons.httpclient.HttpURL(username, password,
            hostname, port, strPath);

      }
      else {
        //instantiate the email object for https protocol
        url = new org.apache.commons.httpclient.HttpsURL(username, password,
            hostname, port, strPath);

      }
    }
    catch (Exception ex) {
      url = null;
    }

    return url;

  }
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 10907006
where username is administrative user account of ms exchange server
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 10907010
say suppose if i want to access the particular email from inbox folder

i have to create object some thing like this

org.apache.commons.httpclient.HttpURL urlEML = getURL("comp37:80/Exchange/Sudhakar_Koundinya/Inbox/YourInfo.EML");
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10907045
No i meant the *value* of that path
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10907050
OK - cross-posted. Shall think about this
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 10907056
ok
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10908042
a. are you sure that the port is correct for your https?
b. make sure all exceptions are being reported by printing their stack traces
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 92

Accepted Solution

by:
objects earned 100 total points
ID: 10909600
try:

webDavResourceFile .getMethod(strAttachmentFile);
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 10915332
@CEHJ
I tried all the possible ways that are applicable. And also I said that emails are downloading, the only problem is with attachments. SO there is no problem with port

@objects
I am using the same method for downloading either emails or attachments.
And again I am saying that the code is working for HTTP protocol. And even in HTTPS i am able to download emails only problem is with attachments download

thanks
sudhakar
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10918120
>>And also I said that emails are downloading, the only problem is with attachments.

Emails and attachments are one and the same thing. When you see attachment icons in your mail client that is only a 'view' thing, not a model thing. The attachments are embedded in the email.

An eml file is not an attachment really - it's a mail that's been saved as a file.
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 10925033
Yes I do agree


but the way  WebDAV resources behaviour is quite different here, MS-Exchange WebDAV server gives the responses in XML format. As attachment of email downloading is different response, MS-Exchange server WebDAV server sends the responses as different XML strings.

1st XML string contains the text part and email attributes such as receiver name, sender name, date ,subject,body and so on

2nd xml string contains name of email attachment, content-type of attachment and so on.

We need to parse the 1st xml string for getting entire email content and I am able to do both it for both  http and https servers

And We need to parse this 2nd xml string for getting the attachments. And I was able to do with HTTP attachment downloads without any problem.
In Https request also i am getting the right xml information. I am able to identify correct paths of attachments. But the problem I was facing was I am not able to get the content of that attachments.

Hope you understand this

Thanks,
Sudhakar
0
 
LVL 92

Expert Comment

by:objects
ID: 10925066
Yes I figured all that.
An aside, did you try the method I posted above? Saves you handling the file download yourself.
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 10925106
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 10925114
Yes objects

I am doing in the same way
0
 
LVL 92

Expert Comment

by:objects
ID: 10925148
when u access the xml using https does the href have http or https?
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 10925159
this is some code snippet

 org.apache.commons.httpclient.HttpURL urlEML = getURL(path + "/" +
          mailbox + "/" + folder + "/" + filename + "/" + strAttachmentName +
          "/");

      org.apache.webdav.lib.WebdavResource webDavResourceFile = getWebResource(
          urlEML);
/************  ATTACHMENT DOWN LOAD******************/
      //get the attachment
      InputStream attachmentInStream = webDavResourceFile.getMethodData();

/************  END OF ATTACHMENT DOWNLOAD ******************/
     
String strAttachmentFile = targetPath + "/" + mailbox + "/" + folder +
          "/" + filename.replaceAll(":", "%3A") + "_Dir/" + strAttachmentName;

      FileOutputStream attachmentOutStream = new FileOutputStream(
          strAttachmentFile);
      int offset = 0;
      //read the buffer from http stream
      while (true) {
        int availByteCount = attachmentInStream.available();

        if (availByteCount <= 0) {
          //end of attachment buffer
          break;
        }
        byte buffer[] = new byte[availByteCount];
        offset = attachmentInStream.read(buffer);
        //write the downloaded attachment buffer in the Local System Object
        attachmentOutStream.write(buffer, 0, offset);
        buffer = null;
      }
      //clear the buffer;
      attachmentOutStream.close();
      attachmentInStream.close();
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 10925177

https
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 10925194
I just got a mail from Apache Slide -Mail Archive

Hi Sudhakar,

I reply you in private because i think it is not a general slide
problem.
I use slide 2.0 rc1 to connect to exchange server both in http and https
and it works perfectly.
To read mail attachment, i use X-MS-ENUMATTACH method and then i simply
use a HttpClient (indeed this is a clone of session of WebdavResource)
and a GetMethod on it and it works fine, i use this because i've extends
WebdavResource class and when i get attachments i don't want that the
status of the resource change.

Do you have any exception when you instantiate the WebdavResource ?

Are you sure that your certificate of your server is valid (and sure
that java will find it valid)?

Try this piece of code to check your certificate :
   
     HttpClient client = new HttpClient();
     GetMethod get = new GetMethod("https://yourserver.com");
     try
     {
       client.executeMethod(get);
     }
     catch ( IOException e )
     {
       e.printStackTrace();
     }
     System.out.println( get.getResponseBodyAsString() );


0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 10925204
do you have any idea about X-MS-ENUMATTACH
0
 
LVL 92

Expert Comment

by:objects
ID: 10925378
> webDavResourceFile.getMethodData();

thought you said you were using getMethod.
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 10925764
What is the difference ?
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 10925773
I mean what is the difference in usage of these methods?
0
 
LVL 92

Expert Comment

by:objects
ID: 10925823
saves you having to worry about reading the stream yourself
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 10925936
http://jakarta.apache.org/slide/clientjavadoc/org/apache/webdav/lib/WebdavResource.html#method_summary

According to the API

boolean       getMethod(java.io.File file)
          Execute the GET method for this WebdavResource path.


java.io.InputStream       getMethodData()
          Get InputStream for the GET method.


I think getMethod is not the correct one for testing.

Any How let me test again with another one

getMethod(java.lang.String path, java.io.File file)


0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 10938016
No Objects,

getMethod is not the right one, it takes WebDAVResourceFile as an argument
0
 
LVL 92

Expert Comment

by:objects
ID: 10944559
no it takes a java.io.File.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now